-- Reverses 045_epa_gap_fill.

DELETE FROM paliad.deadline_rules
 WHERE proceeding_type_id IN (
        SELECT id FROM paliad.proceeding_types WHERE code IN ('EPA_OPP','EPA_APP')
       )
   AND code IN ('epa_opp.r79_further','epa_opp.r116','epa_app.r116','epa_app.r106');

-- Restore epa_app.erwidg to its pre-fix shape.
UPDATE paliad.deadline_rules
   SET name              = 'Erwiderung',
       name_en           = 'Response',
       parent_id         = NULL,
       duration_value    = 0,
       duration_unit     = 'months',
       primary_party     = 'both',
       event_type        = 'filing',
       rule_code         = NULL,
       legal_source      = NULL,
       deadline_notes    = NULL,
       deadline_notes_en = NULL,
       concept_id        = NULL
 WHERE proceeding_type_id = (SELECT id FROM paliad.proceeding_types WHERE code = 'EPA_APP')
   AND code = 'epa_app.erwidg';

-- Restore epa_opp.beschwerde + beschwerde_begr to parent_id=NULL.
UPDATE paliad.deadline_rules
   SET parent_id = NULL
 WHERE proceeding_type_id = (SELECT id FROM paliad.proceeding_types WHERE code = 'EPA_OPP')
   AND code IN ('epa_opp.beschwerde', 'epa_opp.beschwerde_begr');

-- Restore anchor party/event_type.
UPDATE paliad.deadline_rules
   SET primary_party     = 'court',
       event_type        = 'decision',
       deadline_notes    = NULL,
       deadline_notes_en = NULL
 WHERE proceeding_type_id = (SELECT id FROM paliad.proceeding_types WHERE code = 'EPA_OPP')
   AND code = 'epa_opp.grant';

UPDATE paliad.deadline_rules
   SET primary_party     = 'court',
       event_type        = 'decision',
       name              = 'Zustellung der Entscheidung',
       name_en           = 'Notification of Decision',
       deadline_notes    = NULL,
       deadline_notes_en = NULL
 WHERE proceeding_type_id = (SELECT id FROM paliad.proceeding_types WHERE code = 'EPA_APP')
   AND code = 'epa_app.entsch';

DELETE FROM paliad.deadline_concepts
 WHERE slug IN ('r79-further-stellungnahme','r116-final-submissions','petition-for-review')
   AND id NOT IN (SELECT concept_id FROM paliad.deadline_rules WHERE concept_id IS NOT NULL);
