-- t-paliad-200 down — reverses 093_retire_litigation_category.up.sql.
--
-- Restores the 7 litigation-category paliad.proceeding_types rows from
-- the _pre_093 snapshot, moves the 40 archived deadline_rules back onto
-- their original proceeding_type_id values (and reverts
-- lifecycle_state + is_active to their pre-093 values), then drops the
-- _archived_litigation holding pt.
--
-- The snapshot tables themselves stay — they're the source of this
-- rollback's data and a permanent audit artefact. A focused
-- follow-up drops the snapshots once Slice 9 is verified in prod.

SELECT set_config(
    'paliad.audit_reason',
    'rollback 093: restore litigation proceeding_types + un-archive the 40 Pipeline-A rules from pre-093 snapshots',
    true);

-- =============================================================================
-- 1. Restore the 7 litigation proceeding_types rows. ON CONFLICT (id)
--    DO NOTHING — if a row somehow survived the up migration we don't
--    clobber it.
-- =============================================================================

INSERT INTO paliad.proceeding_types
    (id, code, name, description, jurisdiction, category,
     default_color, sort_order, is_active, name_en, display_order)
SELECT id, code, name, description, jurisdiction, category,
       default_color, sort_order, is_active, name_en, display_order
  FROM paliad.proceeding_types_pre_093
 ON CONFLICT (id) DO NOTHING;

-- Re-align the proceeding_types_id_seq if a SERIAL/IDENTITY column
-- bumped past the restored ids. The pre-093 max was 7; the
-- _archived_litigation INSERT in the up migration claimed a later id.
-- Setting the seq to the max of the live table keeps future INSERTs
-- safe regardless of order.
SELECT setval(
    pg_get_serial_sequence('paliad.proceeding_types', 'id'),
    GREATEST(
        (SELECT COALESCE(MAX(id), 1) FROM paliad.proceeding_types),
        1
    )
);

-- =============================================================================
-- 2. Restore the 40 deadline_rules rows to their pre-093 state:
--    proceeding_type_id, lifecycle_state, is_active, updated_at. The
--    rule UUIDs are stable so we match on id. The mig 079 audit
--    trigger captures these UPDATEs as the rollback record.
-- =============================================================================

UPDATE paliad.deadline_rules dr
   SET proceeding_type_id = snap.proceeding_type_id,
       lifecycle_state    = snap.lifecycle_state,
       is_active          = snap.is_active,
       updated_at         = snap.updated_at
  FROM paliad.deadline_rules_pre_093 snap
 WHERE dr.id = snap.id;

-- =============================================================================
-- 3. Drop the _archived_litigation holding pt. Safe — step 2 moved all
--    40 rules off it. The CASCADE is a no-op (FK on rules has
--    ON DELETE CASCADE, but there are zero rules to cascade).
-- =============================================================================

DELETE FROM paliad.proceeding_types
 WHERE code = '_archived_litigation';
