-- Reverses mig 133. Removes the 5 new rules:
--   * upc.dmgs.cfi.interim
--   * upc.dmgs.cfi.oral
--   * upc.dmgs.cfi.decision
--   * upc.dmgs.cfi.appeal_spawn
--   * upc.pi.cfi.appeal_spawn
--
-- The audit_reason is required by the mig 079 trigger for DELETE;
-- set_config at top supplies it.
--
-- Idempotent — if a rule is already missing the DELETE matches zero
-- rows and the audit log records nothing extra.

SELECT set_config(
    'paliad.audit_reason',
    'mig 133 (down): revert UPC Damages tree-end rows and UPC PI appeal-spawn (t-paliad-285 / m/paliad#117 + t-paliad-286 / m/paliad#118)',
    true);

-- Delete the spawn rows first so the parent_id reference goes away
-- before the parent decision row is removed.
DELETE FROM paliad.deadline_rules
 WHERE submission_code IN (
        'upc.dmgs.cfi.appeal_spawn',
        'upc.pi.cfi.appeal_spawn')
   AND lifecycle_state = 'published';

DELETE FROM paliad.deadline_rules
 WHERE submission_code IN (
        'upc.dmgs.cfi.interim',
        'upc.dmgs.cfi.oral',
        'upc.dmgs.cfi.decision')
   AND proceeding_type_id = 17
   AND lifecycle_state = 'published';
