-- Revert t-paliad-263 Wave 0 + m/paliad#99.
-- Restores each Tier 0 row to its pre-fix state per
-- docs/research-deadlines-completeness-2026-05-25.md §10. T0.5 and
-- T0.6 are NOT reverted here — they live in mig 124's down.
--
-- audit_reason set_config required for the mig 079 trigger.

SELECT set_config(
    'paliad.audit_reason',
    'mig 127 revert: unwind Tier 0 deadline-rule corrections (Wave 0 + #99)',
    true);

-- T0.1 defence: 2mo + RoP.049.1 → 3mo + RoP.49.1
UPDATE paliad.deadline_rules
   SET duration_value = 3,
       rule_code      = 'RoP.49.1',
       updated_at     = now()
 WHERE submission_code = 'upc.rev.cfi.defence'
   AND is_active = true
   AND lifecycle_state = 'published';

-- T0.2 rejoin: 1mo + RoP.052/UPC.RoP.52 → 2mo + NULL/NULL
UPDATE paliad.deadline_rules
   SET duration_value = 2,
       rule_code      = NULL,
       legal_source   = NULL,
       updated_at     = now()
 WHERE submission_code = 'upc.rev.cfi.rejoin'
   AND is_active = true
   AND lifecycle_state = 'published';

-- T0.3 response: 3mo + RoP.235.1 → 2mo + NULL
UPDATE paliad.deadline_rules
   SET duration_value = 2,
       rule_code      = NULL,
       legal_source   = NULL,
       updated_at     = now()
 WHERE submission_code = 'upc.apl.merits.response'
   AND is_active = true
   AND lifecycle_state = 'published';

-- T0.4 beruf_begr: parent_id NULL → de.inf.lg.berufung
UPDATE paliad.deadline_rules
   SET parent_id = (
       SELECT id FROM paliad.deadline_rules
        WHERE submission_code = 'de.inf.lg.berufung'
          AND is_active = true
          AND lifecycle_state = 'published'
        LIMIT 1
   ),
   updated_at = now()
 WHERE submission_code = 'de.inf.lg.beruf_begr'
   AND is_active = true
   AND lifecycle_state = 'published';

-- T0.7 reply: clear citation
UPDATE paliad.deadline_rules
   SET rule_code    = NULL,
       legal_source = NULL,
       updated_at   = now()
 WHERE submission_code = 'upc.rev.cfi.reply'
   AND is_active = true
   AND lifecycle_state = 'published';

-- T0.9 notice: revert citation
UPDATE paliad.deadline_rules
   SET rule_code    = 'RoP.220.1',
       legal_source = 'UPC.RoP.220.1',
       updated_at   = now()
 WHERE submission_code = 'upc.apl.merits.notice'
   AND is_active = true
   AND lifecycle_state = 'published';

-- T0.10 grounds: revert citation
UPDATE paliad.deadline_rules
   SET rule_code    = 'RoP.220.1',
       legal_source = 'UPC.RoP.220.1',
       updated_at   = now()
 WHERE submission_code = 'upc.apl.merits.grounds'
   AND is_active = true
   AND lifecycle_state = 'published';

-- T0.12 dpma.opp erwiderung: restore court-set=false + §59 citation
UPDATE paliad.deadline_rules
   SET is_court_set = false,
       rule_code    = '§ 59 PatG',
       legal_source = 'DE.PatG.59.3',
       updated_at   = now()
 WHERE submission_code = 'dpma.opp.dpma.erwiderung'
   AND is_active = true
   AND lifecycle_state = 'published';

-- T0.13 dpma.appeal.bpatg begründung: restore court-set=false + §75 citation
UPDATE paliad.deadline_rules
   SET is_court_set = false,
       rule_code    = '§ 75 PatG',
       legal_source = 'DE.PatG.75.1',
       updated_at   = now()
 WHERE submission_code = 'dpma.appeal.bpatg.begruendung'
   AND is_active = true
   AND lifecycle_state = 'published';

-- T0.14 bpatg erwidg: revert citation
UPDATE paliad.deadline_rules
   SET rule_code    = '§ 82 PatG',
       legal_source = 'DE.PatG.82.1',
       updated_at   = now()
 WHERE submission_code = 'de.null.bpatg.erwidg'
   AND is_active = true
   AND lifecycle_state = 'published';

-- T0.15 bgh begründung: revert citation
UPDATE paliad.deadline_rules
   SET rule_code    = '§ 111 PatG',
       legal_source = 'DE.PatG.111.1',
       updated_at   = now()
 WHERE submission_code = 'de.null.bgh.begruendung'
   AND is_active = true
   AND lifecycle_state = 'published';

-- T0.16 bgh erwiderung: revert court-set + citation
UPDATE paliad.deadline_rules
   SET is_court_set = false,
       rule_code    = '§ 111 PatG',
       legal_source = 'DE.PatG.111.3',
       updated_at   = now()
 WHERE submission_code = 'de.null.bgh.erwiderung'
   AND is_active = true
   AND lifecycle_state = 'published';

-- T0.17 epa.opp opd erwidg: revert court-set
UPDATE paliad.deadline_rules
   SET is_court_set = false,
       updated_at   = now()
 WHERE submission_code = 'epa.opp.opd.erwidg'
   AND is_active = true
   AND lifecycle_state = 'published';

-- #99 upc.inf.cfi.soc: clear citation backfill
UPDATE paliad.deadline_rules
   SET rule_code    = NULL,
       legal_source = NULL,
       updated_at   = now()
 WHERE submission_code = 'upc.inf.cfi.soc'
   AND is_active = true
   AND lifecycle_state = 'published';
