-- ================================================================
-- Migration 004: Fix Processor Results Schema
-- Version: 004
-- Date: 2026-03-26
-- Description: Add missing output_data column and fix worker integration
-- ================================================================

-- 4.1.1: Add output_data column (JSONB) to processor_results
ALTER TABLE processor_results ADD COLUMN IF NOT EXISTS output_data JSONB;

-- 4.1.2: Update processor_results table - drop duration_secs column if exists (we'll compute it)
ALTER TABLE processor_results DROP COLUMN IF EXISTS duration_secs;

-- 4.1.3: Add computed duration column (stored as integer seconds)
ALTER TABLE processor_results ADD COLUMN IF NOT EXISTS duration_secs INT GENERATED ALWAYS AS (
    CASE 
        WHEN completed_at IS NOT NULL AND started_at IS NOT NULL 
        THEN EXTRACT(EPOCH FROM (completed_at - started_at))::INT
        ELSE NULL
    END
) STORED;

-- 4.1.4: Add check constraint for processor values
ALTER TABLE processor_results DROP CONSTRAINT IF EXISTS chk_processor_results_processor;
ALTER TABLE processor_results ADD CONSTRAINT chk_processor_results_processor 
    CHECK (processor IN ('asr', 'cut', 'yolo', 'ocr', 'face', 'pose', 'asrx'));

-- 4.1.5: Create index on processor_results.output_data for JSON queries (optional)
CREATE INDEX IF NOT EXISTS idx_processor_results_output_data ON processor_results USING gin (output_data);

-- 4.1.6: Add foreign key from processor_results.video_id to videos.id if not exists
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.table_constraints 
        WHERE table_name = 'processor_results' 
        AND constraint_name = 'processor_results_video_id_fkey'
    ) THEN
        ALTER TABLE processor_results ADD CONSTRAINT processor_results_video_id_fkey 
            FOREIGN KEY (video_id) REFERENCES videos(id) ON DELETE CASCADE;
    END IF;
END $$;

-- 4.1.7: Update monitor_jobs table - ensure processors column is correct type
ALTER TABLE monitor_jobs ALTER COLUMN processors TYPE VARCHAR(20)[] USING processors::VARCHAR(20)[];
ALTER TABLE monitor_jobs ALTER COLUMN completed_processors TYPE VARCHAR(20)[] USING completed_processors::VARCHAR(20)[];
ALTER TABLE monitor_jobs ALTER COLUMN failed_processors TYPE VARCHAR(20)[] USING failed_processors::VARCHAR(20)[];

-- 4.1.8: Add default values for arrays
ALTER TABLE monitor_jobs ALTER COLUMN processors SET DEFAULT '{"asr","cut","yolo","ocr","face","pose","asrx"}';
ALTER TABLE monitor_jobs ALTER COLUMN completed_processors SET DEFAULT '{}';
ALTER TABLE monitor_jobs ALTER COLUMN failed_processors SET DEFAULT '{}';

-- 4.1.9: Update existing rows to have default processor array
UPDATE monitor_jobs SET processors = '{"asr","cut","yolo","ocr","face","pose","asrx"}' WHERE processors IS NULL;

-- 4.1.10: Add index on monitor_jobs.processors for faster array operations
CREATE INDEX IF NOT EXISTS idx_monitor_jobs_processors ON monitor_jobs USING gin (processors);

COMMENT ON COLUMN processor_results.output_data IS 'JSON output from processor execution';
COMMENT ON COLUMN processor_results.duration_secs IS 'Computed duration in seconds (completed - started)';