-- AEG-X-004: align shadow_run status constraint with the existing Queued application state.
-- Prior migrations are immutable; this is an append-only correction migration.
DO $$
DECLARE
    shadow_run_oid oid := 'model_operations.shadow_run'::regclass;
BEGIN
    IF shadow_run_oid IS NULL THEN
        RAISE EXCEPTION 'model_operations.shadow_run must exist before 0032';
    END IF;

    IF EXISTS (
        SELECT 1
        FROM pg_constraint
        WHERE conrelid = shadow_run_oid
          AND conname = 'check_status'
    ) THEN
        ALTER TABLE model_operations.shadow_run DROP CONSTRAINT check_status;
    END IF;

    IF NOT EXISTS (
        SELECT 1
        FROM pg_constraint
        WHERE conrelid = shadow_run_oid
          AND conname = 'check_status'
    ) THEN
        ALTER TABLE model_operations.shadow_run
            ADD CONSTRAINT check_status CHECK (
                status IN ('Pending', 'Queued', 'DataBackfill', 'Replay', 'EvaluationComplete', 'Failed')
            );
    END IF;
END $$;
