-- Manual rollback for migration 010. Never run after direct MCP data exists.

BEGIN;

DO $$
BEGIN
    IF current_database() <> 'booking_test' THEN
        RAISE EXCEPTION
            'ARR MCP result ingestion rollback is allowed only in booking_test';
    END IF;
    IF to_regclass('ingestion.result_submission_grants') IS NULL
       OR to_regclass('ingestion.result_submissions') IS NULL THEN
        RAISE EXCEPTION
            'ARR MCP result ingestion migration 010 is not applied';
    END IF;
    IF EXISTS (SELECT 1 FROM ingestion.result_submissions)
       OR EXISTS (SELECT 1 FROM ingestion.result_submission_grants)
       OR EXISTS (
           SELECT 1
           FROM ingestion.processing_runs
           WHERE result_delivery_mode = 'direct_mcp'
       )
       OR EXISTS (
           SELECT 1
           FROM finance.daily_versions
           WHERE result_delivery_mode = 'direct_mcp'
       ) THEN
        RAISE EXCEPTION
            'refusing rollback: migration 010 direct MCP state exists';
    END IF;
END;
$$;

DROP TABLE ingestion.result_submissions;
DROP TABLE ingestion.result_submission_grants;

ALTER TABLE finance.daily_versions
    DROP CONSTRAINT daily_versions_status_shape,
    DROP CONSTRAINT daily_versions_result_delivery_shape,
    DROP CONSTRAINT daily_versions_result_delivery_mode_check,
    DROP COLUMN result_delivery_mode;

ALTER TABLE finance.daily_versions
    ADD CONSTRAINT daily_versions_status_shape CHECK (
        (
            version_status IN ('validated', 'active', 'superseded')
            AND business_date IS NOT NULL
            AND daily_report_artifact_id IS NOT NULL
            AND result_json_artifact_id IS NOT NULL
            AND structured_result_artifact_id IS NOT NULL
            AND exception_report_artifact_id IS NULL
            AND failure_code IS NULL
            AND validated_at IS NOT NULL
        )
        OR (
            version_status = 'rejected'
            AND failure_code IS NOT NULL
        )
    );

ALTER TABLE ingestion.processing_runs
    DROP CONSTRAINT processing_runs_terminal_shape,
    DROP CONSTRAINT processing_runs_result_delivery_pipeline_check,
    DROP CONSTRAINT processing_runs_result_delivery_mode_check,
    DROP COLUMN result_delivery_mode;

ALTER TABLE ingestion.processing_runs
    ADD CONSTRAINT processing_runs_terminal_shape CHECK (
        (
            run_status = 'accepted'
            AND failure_code IS NULL
            AND validated_at IS NOT NULL
            AND finished_at IS NOT NULL
            AND (
                pipeline_type <> 'opera_daily'
                OR (
                    result_artifact_id IS NOT NULL
                    AND business_date IS NOT NULL
                    AND delivered_processor_version IS NOT NULL
                    AND delivered_rule_set_sha256 IS NOT NULL
                    AND result_schema_version IS NOT NULL
                    AND delivery_sha256 IS NOT NULL
                )
            )
        )
        OR (
            run_status IN ('rejected', 'failed')
            AND failure_code IS NOT NULL
            AND finished_at IS NOT NULL
        )
        OR (
            run_status = 'cancelled'
            AND finished_at IS NOT NULL
        )
        OR run_status IN ('received', 'queued', 'running', 'validating')
    );

COMMIT;
