-- 003_create_media_objects.sql
-- Media metadata table for tracking uploads, generation provenance, and soft deletes.
-- Compatible with both PostgreSQL (local dev) and CockroachDB (production).

CREATE TABLE IF NOT EXISTS media_objects (
    id                  TEXT PRIMARY KEY,
    user_id             TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    path                TEXT NOT NULL UNIQUE,
    filename            TEXT NOT NULL DEFAULT '',
    content_type        TEXT NOT NULL DEFAULT '',
    size                BIGINT NOT NULL DEFAULT 0,
    generation_job_id   TEXT NOT NULL DEFAULT '',
    deleted_at          TIMESTAMPTZ,
    created_at          TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at          TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_media_objects_user_id ON media_objects (user_id, created_at DESC)
    WHERE deleted_at IS NULL;

CREATE INDEX IF NOT EXISTS idx_media_objects_generation_job ON media_objects (generation_job_id)
    WHERE generation_job_id != '';
