-- Reverses 029_tier1_rule_fixes. Rule_code normalisation is intentionally
-- one-way; the down-step only restores the parent_id chain on app.grounds
-- so an out-of-band rollback can resume the prior shape if needed. The
-- new columns (condition_flag, anchor_alt) stay on the table — harmless
-- when null and removing them would force re-applying the data fixes.

UPDATE paliad.deadline_rules
   SET parent_id      = (SELECT id FROM paliad.deadline_rules
                          WHERE proceeding_type_id = (SELECT id FROM paliad.proceeding_types WHERE code = 'UPC_APP')
                            AND code = 'app.notice'),
       duration_value = 2,
       deadline_notes = NULL
 WHERE proceeding_type_id = (SELECT id FROM paliad.proceeding_types WHERE code = 'UPC_APP')
   AND code = 'app.grounds';

UPDATE paliad.deadline_rules
   SET parent_id      = (SELECT id FROM paliad.deadline_rules
                          WHERE proceeding_type_id = (SELECT id FROM paliad.proceeding_types WHERE code = 'APP')
                            AND code = 'app.notice'),
       duration_value = 2,
       deadline_notes = NULL
 WHERE proceeding_type_id = (SELECT id FROM paliad.proceeding_types WHERE code = 'APP')
   AND code = 'app.grounds';

UPDATE paliad.deadline_rules
   SET condition_flag    = NULL,
       alt_duration_value = NULL,
       alt_duration_unit  = NULL,
       alt_rule_code      = NULL
 WHERE proceeding_type_id = (SELECT id FROM paliad.proceeding_types WHERE code = 'UPC_INF')
   AND code IN ('inf.reply', 'inf.rejoin');

UPDATE paliad.deadline_rules
   SET anchor_alt     = NULL,
       deadline_notes = 'Ab Prioritätstag'
 WHERE proceeding_type_id = (SELECT id FROM paliad.proceeding_types WHERE code = 'EP_GRANT')
   AND code = 'ep_grant.publish';
