-- Down: restore prior names + drop display_order column.
BEGIN;

UPDATE paliad.proceeding_types SET name = 'Nichtigkeitsklage'              WHERE code = 'UPC_REV';
UPDATE paliad.proceeding_types SET name = 'Berufung'                       WHERE code = 'UPC_APP';
UPDATE paliad.proceeding_types SET name = 'Verletzungsklage (LG)'          WHERE code = 'DE_INF';
UPDATE paliad.proceeding_types SET name = 'Berufung OLG (Verletzung)'      WHERE code = 'DE_INF_OLG';
UPDATE paliad.proceeding_types SET name = 'Berufung BGH (Nichtigkeit)'     WHERE code = 'DE_NULL_BGH';
UPDATE paliad.proceeding_types SET name = 'Beschwerde BPatG (DPMA)'        WHERE code = 'DPMA_BPATG_BESCHWERDE';
UPDATE paliad.proceeding_types SET name = 'Einspruch DPMA'                 WHERE code = 'DPMA_OPP';
UPDATE paliad.proceeding_types SET name = 'Berufung Kostenentscheidung'    WHERE code = 'UPC_COST_APPEAL';
UPDATE paliad.proceeding_types SET name = 'Berufung Anordnungen'           WHERE code = 'UPC_APP_ORDERS';
UPDATE paliad.proceeding_types SET name = 'Rechtsbeschwerde BGH'           WHERE code = 'DPMA_BGH_RB';
UPDATE paliad.proceeding_types SET name = 'Revision/NZB BGH (Verletzung)'  WHERE code = 'DE_INF_BGH';

UPDATE paliad.deadline_rules dr
   SET legal_source = NULL,
       rule_code    = NULL
  FROM paliad.proceeding_types p
 WHERE dr.proceeding_type_id = p.id
   AND p.code = 'UPC_REV'
   AND dr.code = 'rev.defence'
   AND dr.legal_source = 'UPC.RoP.49.1';

-- Recreate matview without proceeding_display_order, then drop column.
DROP MATERIALIZED VIEW IF EXISTS paliad.deadline_search;
CREATE MATERIALIZED VIEW paliad.deadline_search AS
SELECT
    'rule'::text                         AS kind,
    'r:' || dr.id::text                  AS row_key,
    dc.id                                AS concept_id,
    dc.slug                              AS concept_slug,
    dc.name_de                           AS concept_name_de,
    dc.name_en                           AS concept_name_en,
    dc.description                       AS concept_description,
    dc.aliases                           AS concept_aliases,
    dc.party                             AS concept_party,
    dc.category                          AS concept_category,
    dc.sort_order                        AS concept_sort_order,
    dr.id                                AS rule_id,
    NULL::bigint                         AS trigger_event_id,
    pt.code                              AS proceeding_code,
    pt.name                              AS proceeding_name_de,
    pt.name_en                           AS proceeding_name_en,
    pt.jurisdiction                      AS jurisdiction,
    dr.code                              AS rule_local_code,
    dr.name                              AS rule_name_de,
    dr.name_en                           AS rule_name_en,
    dr.legal_source                      AS legal_source,
    dr.rule_code                         AS rule_code,
    dr.duration_value,
    dr.duration_unit,
    dr.timing,
    COALESCE(dr.primary_party, dc.party) AS effective_party
  FROM paliad.deadline_rules dr
  JOIN paliad.proceeding_types pt  ON pt.id = dr.proceeding_type_id
  JOIN paliad.deadline_concepts dc ON dc.id = dr.concept_id
 WHERE dr.is_active AND pt.is_active AND pt.category = 'fristenrechner'
UNION ALL
SELECT
    'trigger'::text, 't:' || te.id::text, dc.id, dc.slug, dc.name_de,
    dc.name_en, dc.description, dc.aliases, dc.party, dc.category,
    dc.sort_order, NULL::uuid, te.id, NULL::text, NULL::text, NULL::text,
    'cross-cutting'::text, te.code, te.name_de, te.name, NULL::text,
    NULL::text, NULL::int, NULL::text, NULL::text, dc.party
  FROM paliad.trigger_events te
  JOIN paliad.deadline_concepts dc ON dc.slug = te.concept_id
 WHERE te.is_active;
CREATE UNIQUE INDEX deadline_search_row_key       ON paliad.deadline_search (row_key);
CREATE INDEX deadline_search_concept_id           ON paliad.deadline_search (concept_id);
CREATE INDEX deadline_search_proc_code            ON paliad.deadline_search (proceeding_code);
CREATE INDEX deadline_search_legal_source         ON paliad.deadline_search (legal_source);
CREATE INDEX deadline_search_effective_party      ON paliad.deadline_search (effective_party);
CREATE INDEX deadline_search_legal_source_trgm    ON paliad.deadline_search USING gin (legal_source gin_trgm_ops);
CREATE INDEX deadline_search_concept_de_trgm      ON paliad.deadline_search USING gin (concept_name_de gin_trgm_ops);
CREATE INDEX deadline_search_concept_en_trgm      ON paliad.deadline_search USING gin (concept_name_en gin_trgm_ops);
CREATE INDEX deadline_search_rule_de_trgm         ON paliad.deadline_search USING gin (rule_name_de gin_trgm_ops);
CREATE INDEX deadline_search_rule_en_trgm         ON paliad.deadline_search USING gin (rule_name_en gin_trgm_ops);
CREATE INDEX deadline_search_rule_code_trgm       ON paliad.deadline_search USING gin (rule_code gin_trgm_ops);

ALTER TABLE paliad.proceeding_types DROP COLUMN IF EXISTS display_order;

COMMIT;
