-- Reverses mig 096. Restores the original UPPER_SNAKE codes on
-- paliad.proceeding_types + paliad.event_category_concepts, drops the
-- new upc.ccr.cfi row, removes the shape CHECK, refreshes the
-- deadline_search materialized view, then drops the snapshot table.
--
-- audit_reason wrapper required by the mig 079 audit trigger.

SELECT set_config(
    'paliad.audit_reason',
    'mig 096 (down): revert t-paliad-206 proceeding-code rename — restore UPPER_SNAKE codes from proceeding_types_pre_096, delete upc.ccr.cfi peer, drop shape CHECK',
    true);

-- =============================================================================
-- 1. Drop the shape CHECK first so the UPPER_SNAKE restores don't trip it.
-- =============================================================================

ALTER TABLE paliad.proceeding_types
    DROP CONSTRAINT IF EXISTS paliad_proceeding_code_shape;

-- =============================================================================
-- 2. Delete the upc.ccr.cfi peer. The down restores the pre-096 state, which
--    didn't have this row. If the row is already missing, the DELETE
--    matches zero — idempotent.
-- =============================================================================

DELETE FROM paliad.proceeding_types
 WHERE code = 'upc.ccr.cfi';

-- =============================================================================
-- 3. Restore proceeding_types.code from the pre_096 snapshot. The snapshot
--    captured the rows at first up-migration run; if the table is missing
--    (down run before up), the restore is a no-op.
-- =============================================================================

DO $$
DECLARE
    v_snap_exists boolean;
BEGIN
    SELECT EXISTS (
        SELECT 1
          FROM information_schema.tables
         WHERE table_schema = 'paliad'
           AND table_name = 'proceeding_types_pre_096'
    ) INTO v_snap_exists;

    IF NOT v_snap_exists THEN
        RAISE NOTICE
            'mig 096 (down): snapshot table paliad.proceeding_types_pre_096 missing — nothing to restore';
        RETURN;
    END IF;

    UPDATE paliad.proceeding_types pt
       SET code = snap.code
      FROM paliad.proceeding_types_pre_096 snap
     WHERE pt.id = snap.id
       AND pt.code <> snap.code;
END $$;

-- =============================================================================
-- 4. Revert soft references on event_category_concepts.proceeding_type_code
--    by running the inverse mapping. Symmetric with §4 of the up migration.
-- =============================================================================

UPDATE paliad.event_category_concepts SET proceeding_type_code = 'UPC_INF'         WHERE proceeding_type_code = 'upc.inf.cfi';
UPDATE paliad.event_category_concepts SET proceeding_type_code = 'UPC_REV'         WHERE proceeding_type_code = 'upc.rev.cfi';
UPDATE paliad.event_category_concepts SET proceeding_type_code = 'UPC_PI'          WHERE proceeding_type_code = 'upc.pi.cfi';
UPDATE paliad.event_category_concepts SET proceeding_type_code = 'UPC_APP'         WHERE proceeding_type_code = 'upc.apl.merits';
UPDATE paliad.event_category_concepts SET proceeding_type_code = 'UPC_DAMAGES'     WHERE proceeding_type_code = 'upc.dmgs.cfi';
UPDATE paliad.event_category_concepts SET proceeding_type_code = 'UPC_DISCOVERY'   WHERE proceeding_type_code = 'upc.disc.cfi';
UPDATE paliad.event_category_concepts SET proceeding_type_code = 'UPC_COST_APPEAL' WHERE proceeding_type_code = 'upc.apl.cost';
UPDATE paliad.event_category_concepts SET proceeding_type_code = 'UPC_APP_ORDERS'  WHERE proceeding_type_code = 'upc.apl.order';

UPDATE paliad.event_category_concepts SET proceeding_type_code = 'DE_INF'      WHERE proceeding_type_code = 'de.inf.lg';
UPDATE paliad.event_category_concepts SET proceeding_type_code = 'DE_INF_OLG'  WHERE proceeding_type_code = 'de.inf.olg';
UPDATE paliad.event_category_concepts SET proceeding_type_code = 'DE_INF_BGH'  WHERE proceeding_type_code = 'de.inf.bgh';
UPDATE paliad.event_category_concepts SET proceeding_type_code = 'DE_NULL'     WHERE proceeding_type_code = 'de.null.bpatg';
UPDATE paliad.event_category_concepts SET proceeding_type_code = 'DE_NULL_BGH' WHERE proceeding_type_code = 'de.null.bgh';

UPDATE paliad.event_category_concepts SET proceeding_type_code = 'EP_GRANT' WHERE proceeding_type_code = 'epa.grant.exa';
UPDATE paliad.event_category_concepts SET proceeding_type_code = 'EPA_OPP'  WHERE proceeding_type_code = 'epa.opp.opd';
UPDATE paliad.event_category_concepts SET proceeding_type_code = 'EPA_APP'  WHERE proceeding_type_code = 'epa.opp.boa';

UPDATE paliad.event_category_concepts SET proceeding_type_code = 'DPMA_OPP'              WHERE proceeding_type_code = 'dpma.opp.dpma';
UPDATE paliad.event_category_concepts SET proceeding_type_code = 'DPMA_BPATG_BESCHWERDE' WHERE proceeding_type_code = 'dpma.appeal.bpatg';
UPDATE paliad.event_category_concepts SET proceeding_type_code = 'DPMA_BGH_RB'           WHERE proceeding_type_code = 'dpma.appeal.bgh';

-- =============================================================================
-- 5. Refresh deadline_search so the reverted proceeding_code strings
--    repopulate the materialized view.
-- =============================================================================

REFRESH MATERIALIZED VIEW paliad.deadline_search;

-- =============================================================================
-- 6. Drop the snapshot table so a re-applied up migration captures a
--    fresh snapshot of the current state.
-- =============================================================================

DROP TABLE IF EXISTS paliad.proceeding_types_pre_096;
