-- Reverses 042_de_expansion_b3.

DELETE FROM paliad.deadline_rules
 WHERE proceeding_type_id IN (
        SELECT id FROM paliad.proceeding_types WHERE code IN ('DE_INF','DE_NULL')
       )
   AND code IN (
        'de_inf.anzeige',
        'de_null.replik_klaeger', 'de_null.hinweisbeschluss',
        'de_null.stellungnahme',  'de_null.duplik'
       );

DELETE FROM paliad.deadline_concepts
 WHERE slug IN ('preliminary-opinion','response-to-preliminary-opinion','notice-of-defence-intention')
   AND id NOT IN (SELECT concept_id FROM paliad.deadline_rules WHERE concept_id IS NOT NULL);

UPDATE paliad.deadline_rules
   SET duration_value = 1,
       deadline_notes    = NULL,
       deadline_notes_en = NULL
 WHERE proceeding_type_id = (SELECT id FROM paliad.proceeding_types WHERE code = 'DE_NULL')
   AND code = 'de_null.beruf_begr';

WITH proc AS (SELECT id FROM paliad.proceeding_types WHERE code = 'DE_INF')
UPDATE paliad.deadline_rules dr
   SET sequence_order = CASE dr.code
       WHEN 'de_inf.klage'      THEN 0
       WHEN 'de_inf.erwidg'     THEN 1
       WHEN 'de_inf.replik'     THEN 2
       WHEN 'de_inf.duplik'     THEN 3
       WHEN 'de_inf.termin'     THEN 4
       WHEN 'de_inf.urteil'     THEN 5
       WHEN 'de_inf.berufung'   THEN 6
       WHEN 'de_inf.beruf_begr' THEN 7
       ELSE dr.sequence_order
   END
  FROM proc p
 WHERE dr.proceeding_type_id = p.id;

WITH proc AS (SELECT id FROM paliad.proceeding_types WHERE code = 'DE_NULL')
UPDATE paliad.deadline_rules dr
   SET sequence_order = CASE dr.code
       WHEN 'de_null.klage'      THEN 0
       WHEN 'de_null.erwidg'     THEN 1
       WHEN 'de_null.termin'     THEN 2
       WHEN 'de_null.urteil'     THEN 3
       WHEN 'de_null.berufung'   THEN 4
       WHEN 'de_null.beruf_begr' THEN 5
       ELSE dr.sequence_order
   END
  FROM proc p
 WHERE dr.proceeding_type_id = p.id;
