-- t-paliad-157 cascade gap (m/paliad#14 section C):
-- The "Frist verpasst" branch surfaced national-DE (PatG / ZPO), EPA and
-- DPMA Wiedereinsetzung paths but had no UPC option. UPC R.320 RoP grants
-- re-establishment of rights with the same shape as the national
-- equivalents (2 months from removal of obstacle, 12-month outer limit
-- from the missed deadline).
--
-- Three additions, all idempotent:
--   1. New trigger_event "Wegfall des Hindernisses (UPC R.320)" tied to
--      the existing wiedereinsetzung concept (so the concept card picks
--      up a UPC pill alongside DE / EPA / DPMA).
--   2. New event_categories leaf frist-verpasst.upc, slotted at
--      sort_order 50 so UPC reads first under "Frist verpasst" (national
--      & supranational siblings remain at 100/200/300/400).
--   3. Junction row linking the new leaf to the wiedereinsetzung concept.
--      NULL proceeding_type_code mirrors the existing siblings — the
--      Wiedereinsetzung pills come from cross-cutting trigger_events
--      that bypass the forum filter by design (search service docs);
--      per-leaf narrowing is part of the IA-reframe issue (#16), not
--      this fix.
--
-- The materialised view paliad.deadline_search refreshes on the next
-- server boot via services.RefreshSearchView (cmd/server/main.go:94),
-- so the new trigger event becomes searchable as soon as the deploy
-- restarts the process. No matview refresh from the migration itself.

INSERT INTO paliad.trigger_events
    (id, code, name, name_de, description, is_active, concept_id)
VALUES
    (207,
     'wegfall_hindernisses_upc',
     'Removal of obstacle (UPC R.320)',
     'Wegfall des Hindernisses (UPC R.320)',
     'Der Tag, an dem das Hindernis weggefallen ist, das die Einhaltung einer UPC-Frist verhinderte (R.320 RoP). Antrag auf Wiedereinsetzung binnen zwei Monaten ab Wegfall, höchstens zwölf Monate ab Ablauf der versäumten Frist.',
     true,
     'wiedereinsetzung')
ON CONFLICT (id) DO UPDATE SET
    code        = EXCLUDED.code,
    name        = EXCLUDED.name,
    name_de     = EXCLUDED.name_de,
    description = EXCLUDED.description,
    is_active   = EXCLUDED.is_active,
    concept_id  = EXCLUDED.concept_id;

INSERT INTO paliad.event_categories
    (slug, parent_id, label_de, label_en, description_de, description_en,
     step_question_de, step_question_en, sort_order, is_leaf, is_active)
VALUES
    ('frist-verpasst.upc',
     (SELECT id FROM paliad.event_categories WHERE slug = 'frist-verpasst'),
     'UPC (R.320 RoP)',
     'UPC (R.320 RoP)',
     'Wiedereinsetzung in den vorigen Stand vor dem Einheitlichen Patentgericht (R.320 RoP). Zwei Monate ab Wegfall des Hindernisses, höchstens zwölf Monate ab Fristablauf.',
     'Re-establishment of rights before the Unified Patent Court (R.320 RoP). Two months from removal of the obstacle, capped at twelve months from the missed deadline.',
     NULL,
     NULL,
     50,
     true,
     true)
ON CONFLICT (slug) DO UPDATE SET
    parent_id        = EXCLUDED.parent_id,
    label_de         = EXCLUDED.label_de,
    label_en         = EXCLUDED.label_en,
    description_de   = EXCLUDED.description_de,
    description_en   = EXCLUDED.description_en,
    step_question_de = EXCLUDED.step_question_de,
    step_question_en = EXCLUDED.step_question_en,
    sort_order       = EXCLUDED.sort_order,
    is_leaf          = EXCLUDED.is_leaf,
    is_active        = EXCLUDED.is_active,
    updated_at       = now();

INSERT INTO paliad.event_category_concepts
    (event_category_id, concept_id, proceeding_type_code, sort_order)
VALUES
    ((SELECT id FROM paliad.event_categories  WHERE slug = 'frist-verpasst.upc'),
     (SELECT id FROM paliad.deadline_concepts WHERE slug = 'wiedereinsetzung'),
     NULL,
     100)
ON CONFLICT DO NOTHING;
