-- Rollback of mig 132 (t-paliad-284 Wave 1 + m/paliad#116).
--
-- Reverses §0 (R.104/R.105 citation backfill) + §1..§11 (11 Tier 1
-- INSERTs) + §12 (T1.12 re-anchor of upc.pi.cfi.response).
--
-- Does NOT reverse §13b (Q6 archived-litigation cleanup) — those rows
-- were already in lifecycle_state='archived' before deletion and are not
-- surfaced by any product code path. Restoring them would require the
-- pre-mig-132 backup. Leaving them gone is the correct rollback choice;
-- emergency restore goes via mig 123 backup snapshot.
--
-- DOES restore §13a (re-add the deadline_rule_audit.rule_id FK) so the
-- audit-table schema returns to its pre-mig-132 shape on rollback. Any
-- orphan audit rows accumulated under mig 132 (rule_id pointing at
-- now-deleted rules) would block the FK re-add; the rollback DELETE
-- below removes them first.

SELECT set_config(
    'paliad.audit_reason',
    'mig 132 down: rollback Wave 1 Tier 1 rule additions + R.105 citation backfill + T1.12 re-anchor (t-paliad-284 / m/paliad#116)',
    true);

-- §12 down — un-re-anchor upc.pi.cfi.response back to its broken root state.
UPDATE paliad.deadline_rules
   SET parent_id    = NULL,
       is_court_set = false,
       rule_code    = NULL,
       legal_source = NULL,
       updated_at   = now()
 WHERE submission_code = 'upc.pi.cfi.response'
   AND is_active = true
   AND lifecycle_state = 'published'
   AND is_court_set = true
   AND rule_code = 'RoP.211.2';

-- §1..§11 down — delete the 11 Tier 1 INSERTs by submission_code.
DELETE FROM paliad.deadline_rules
 WHERE submission_code IN (
     'upc.inf.cfi.cmo_review',
     'upc.inf.cfi.confidentiality_response',
     'upc.apl.order.response_orders',   -- delete child first (FK to grounds_orders)
     'upc.apl.order.grounds_orders',
     'upc.inf.cfi.cons_orders',
     'upc.inf.cfi.rectification',
     'upc.pi.cfi.deficiency',
     'upc.pi.cfi.merits_start',
     'upc.inf.cfi.translation_request',
     'upc.inf.cfi.interpreter_cost',
     'upc.inf.cfi.translations_lodge'
 )
   AND lifecycle_state = 'published';

-- §0 down — clear the R.104/R.105 citation on upc.inf.cfi.interim.
UPDATE paliad.deadline_rules
   SET rule_code    = NULL,
       legal_source = NULL,
       rule_codes   = NULL,
       updated_at   = now()
 WHERE submission_code = 'upc.inf.cfi.interim'
   AND is_active = true
   AND lifecycle_state = 'published'
   AND rule_code = 'RoP.104'
   AND legal_source = 'UPC.RoP.104';

-- §13a down — re-add the deadline_rule_audit.rule_id FK with the
-- original ON DELETE CASCADE shape. Purge any orphan audit rows first
-- (audit entries pointing at rule_ids that no longer exist in
-- deadline_rules) so the FK re-add doesn't fail validation.
DELETE FROM paliad.deadline_rule_audit a
 WHERE NOT EXISTS (
     SELECT 1 FROM paliad.deadline_rules dr WHERE dr.id = a.rule_id
 );

ALTER TABLE paliad.deadline_rule_audit
    ADD CONSTRAINT deadline_rule_audit_rule_id_fkey
    FOREIGN KEY (rule_id) REFERENCES paliad.deadline_rules(id) ON DELETE CASCADE;
