-- 2026-05: Advanced features — trim jobs, temp segments, conform tracking.
-- Idempotent: safe to re-run (IF NOT EXISTS / DO $$ BEGIN guards throughout).

-- 1. Add 'trim' to job_type enum
DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'trim' AND enumtypid = 'job_type'::regtype) THEN
    ALTER TYPE job_type ADD VALUE 'trim';
  END IF;
END $$;

-- 2. Temp segments table — tracks per-clip trimmed hi-res segments
--    with a 24-hour TTL for auto-cleanup.
CREATE TABLE IF NOT EXISTS temp_segments (
  id               UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  job_id           UUID NOT NULL REFERENCES jobs(id) ON DELETE CASCADE,
  clip_instance_id UUID NOT NULL,
  asset_id         UUID NOT NULL REFERENCES assets(id) ON DELETE CASCADE,
  s3_key           TEXT NOT NULL,
  expires_at       TIMESTAMPTZ NOT NULL,
  created_at       TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_temp_segments_expires_at ON temp_segments(expires_at);
CREATE INDEX IF NOT EXISTS idx_temp_segments_job_id      ON temp_segments(job_id);
CREATE INDEX IF NOT EXISTS idx_temp_segments_asset_id     ON temp_segments(asset_id);

-- 3. Asset conform tracking — remember which sequence this asset was
--    conformed from so the UI can show lineage and prevent double-conform.
ALTER TABLE assets ADD COLUMN IF NOT EXISTS conform_source_sequence_id UUID REFERENCES sequences(id);
CREATE INDEX IF NOT EXISTS idx_assets_conform_source ON assets(conform_source_sequence_id);
