CREATE TABLE assets (
    asset_id        UUID PRIMARY KEY,
    volume_id       UUID NOT NULL REFERENCES storage_volumes(volume_id),
    relative_path   TEXT NOT NULL,
    checksum        TEXT NOT NULL,
    asset_type      TEXT NOT NULL,
    mime_type       TEXT NOT NULL,
    file_size       BIGINT NOT NULL,
    is_processed    BOOLEAN NOT NULL DEFAULT false,
    owner_user_id   UUID NOT NULL REFERENCES users(id),
    created_at      TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_assets_checksum ON assets(checksum);
CREATE INDEX idx_assets_owner ON assets(owner_user_id);

CREATE TABLE asset_metadata (
    asset_id         UUID NOT NULL REFERENCES assets(asset_id) ON DELETE CASCADE,
    metadata_source  TEXT NOT NULL,
    data             JSONB NOT NULL DEFAULT '{}',
    updated_at       TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    PRIMARY KEY (asset_id, metadata_source)
);
