-- ================================================================
-- Migration 036: ASR/ASRX and Face Detailed Status
-- Version: 036
-- Date: 2026-06-26
-- Description: Add asr_status and face_status columns for detailed result status
--              to support unified output SOP
-- ================================================================

-- 36.1: Add asr_status column to processor_results
ALTER TABLE processor_results ADD COLUMN IF NOT EXISTS asr_status VARCHAR(20);

COMMENT ON COLUMN processor_results.asr_status IS 
    'ASR-specific status: no_audio_track, silent_audio, has_transcript, processing';

-- 36.2: Add check constraint for asr_status
ALTER TABLE processor_results DROP CONSTRAINT IF EXISTS chk_processor_results_asr_status;
ALTER TABLE processor_results ADD CONSTRAINT chk_processor_results_asr_status 
    CHECK (asr_status IS NULL OR asr_status IN ('no_audio_track', 'silent_audio', 'has_transcript', 'processing'));

-- 36.3: Add segment_count column for quick reference
ALTER TABLE processor_results ADD COLUMN IF NOT EXISTS segment_count INTEGER DEFAULT 0;

COMMENT ON COLUMN processor_results.segment_count IS 
    'Number of transcript segments (ASR) or speaker segments (ASRX)';

-- 36.4: Create index for asr_status queries
CREATE INDEX IF NOT EXISTS idx_processor_results_asr_status ON processor_results(asr_status) 
    WHERE asr_status IS NOT NULL;

-- 36.5: Add face_status column to processor_results
ALTER TABLE processor_results ADD COLUMN IF NOT EXISTS face_status VARCHAR(20);

COMMENT ON COLUMN processor_results.face_status IS 
    'Face detection status: no_faces, has_faces, processing';

-- 36.6: Add check constraint for face_status
ALTER TABLE processor_results DROP CONSTRAINT IF EXISTS chk_processor_results_face_status;
ALTER TABLE processor_results ADD CONSTRAINT chk_processor_results_face_status 
    CHECK (face_status IS NULL OR face_status IN ('no_faces', 'has_faces', 'processing'));

-- 36.7: Add total_faces column for quick reference
ALTER TABLE processor_results ADD COLUMN IF NOT EXISTS total_faces INTEGER DEFAULT 0;

COMMENT ON COLUMN processor_results.total_faces IS 
    'Total number of faces detected across all frames';

-- 36.8: Create index for face_status queries
CREATE INDEX IF NOT EXISTS idx_processor_results_face_status ON processor_results(face_status) 
    WHERE face_status IS NOT NULL;