-- t-paliad-157 / m's 2026-05-08 batch Item 4: more opponent-side
-- proceeding types in the B1 cascade.
--
-- Today `cms-eingang.gegenseite` exposes UPC INF/REV, DE INF/NULL, EPA
-- OPP/APP, DPMA OPP — but is missing the appellate / interim-measures
-- arms m named: UPC Berufung, UPC einstweilige Maßnahmen, DE BGH
-- (Revision/NZB), DE BGH-Berufung Nichtigkeit, DPMA Rechtsbeschwerde.
-- Each parent gets a few concrete leaves wired to the relevant
-- deadline_concepts so the result-card pills land on the right rules.
--
-- forums tag matches the parent jurisdiction so the inbox-channel chip
-- (m/paliad#15) correctly shows / hides each subtree.
--
-- proceeding_type_code on each junction row narrows the result card to
-- the relevant proceeding (so the user picking "DE BGH Revision" sees
-- DE_INF_BGH pills, not all proceedings sharing the underlying
-- concept).
--
-- Idempotent: every INSERT uses ON CONFLICT (slug) DO UPDATE / DO
-- NOTHING so re-running the migration after a partial apply is safe.

-- ============================================================================
-- 1. New parent nodes under cms-eingang.gegenseite
-- ============================================================================

INSERT INTO paliad.event_categories
    (slug, parent_id, label_de, label_en, step_question_de, step_question_en,
     sort_order, is_leaf, is_active, forums)
VALUES
    ('cms-eingang.gegenseite.upc-app',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite'),
     'UPC Berufungsverfahren',
     'UPC Appeal',
     'Welcher Schriftsatz?',
     'Which submission?',
     250, false, true, ARRAY['upc']),

    ('cms-eingang.gegenseite.upc-pi',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite'),
     'UPC einstweilige Maßnahmen',
     'UPC Provisional Measures',
     'Welcher Schriftsatz?',
     'Which submission?',
     280, false, true, ARRAY['upc']),

    ('cms-eingang.gegenseite.de-bgh-inf',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite'),
     'DE Revision / NZB BGH (Verletzung)',
     'DE Revision / NZB BGH (infringement)',
     'Welcher Schriftsatz?',
     'Which submission?',
     320, false, true, ARRAY['de']),

    ('cms-eingang.gegenseite.de-bgh-null',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite'),
     'DE Berufung BGH (Nichtigkeit)',
     'DE Appeal BGH (nullity)',
     'Welcher Schriftsatz?',
     'Which submission?',
     450, false, true, ARRAY['de']),

    ('cms-eingang.gegenseite.dpma-bgh',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite'),
     'DPMA Rechtsbeschwerde BGH',
     'DPMA Rechtsbeschwerde BGH',
     'Welcher Schriftsatz?',
     'Which submission?',
     750, false, true, ARRAY['dpma'])
ON CONFLICT (slug) DO UPDATE SET
    parent_id        = EXCLUDED.parent_id,
    label_de         = EXCLUDED.label_de,
    label_en         = EXCLUDED.label_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,
    forums           = EXCLUDED.forums,
    updated_at       = now();

-- ============================================================================
-- 2. UPC_APP children
-- ============================================================================

INSERT INTO paliad.event_categories
    (slug, parent_id, label_de, label_en, sort_order, is_leaf, is_active, forums)
VALUES
    ('cms-eingang.gegenseite.upc-app.berufungsschrift',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite.upc-app'),
     'Berufungsschrift', 'Notice of Appeal', 100, true, true, ARRAY['upc']),
    ('cms-eingang.gegenseite.upc-app.berufungsbegruendung',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite.upc-app'),
     'Berufungsbegründung', 'Statement of Grounds', 200, true, true, ARRAY['upc']),
    ('cms-eingang.gegenseite.upc-app.berufungserwiderung',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite.upc-app'),
     'Berufungserwiderung', 'Response to Appeal', 300, true, true, ARRAY['upc']),
    ('cms-eingang.gegenseite.upc-app.anschlussberufung',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite.upc-app'),
     'Anschlussberufung (R.237)', 'Cross-Appeal (R.237)', 400, true, true, ARRAY['upc']),
    ('cms-eingang.gegenseite.upc-app.reply-to-cross-appeal',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite.upc-app'),
     'Erwiderung Anschlussberufung (R.238)', 'Reply to Cross-Appeal (R.238)', 500, true, true, ARRAY['upc'])
ON CONFLICT (slug) DO UPDATE SET
    parent_id  = EXCLUDED.parent_id,
    label_de   = EXCLUDED.label_de,
    label_en   = EXCLUDED.label_en,
    sort_order = EXCLUDED.sort_order,
    is_leaf    = EXCLUDED.is_leaf,
    is_active  = EXCLUDED.is_active,
    forums     = EXCLUDED.forums,
    updated_at = now();

-- ============================================================================
-- 3. UPC_PI children
-- ============================================================================

INSERT INTO paliad.event_categories
    (slug, parent_id, label_de, label_en, sort_order, is_leaf, is_active, forums)
VALUES
    ('cms-eingang.gegenseite.upc-pi.antrag',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite.upc-pi'),
     'Antrag auf einstw. Maßnahmen', 'Application for Provisional Measures', 100, true, true, ARRAY['upc']),
    ('cms-eingang.gegenseite.upc-pi.erwiderung',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite.upc-pi'),
     'Erwiderung', 'Statement of Defence (PI)', 200, true, true, ARRAY['upc'])
ON CONFLICT (slug) DO UPDATE SET
    parent_id  = EXCLUDED.parent_id,
    label_de   = EXCLUDED.label_de,
    label_en   = EXCLUDED.label_en,
    sort_order = EXCLUDED.sort_order,
    is_leaf    = EXCLUDED.is_leaf,
    is_active  = EXCLUDED.is_active,
    forums     = EXCLUDED.forums,
    updated_at = now();

-- ============================================================================
-- 4. DE_INF_BGH children
-- ============================================================================

INSERT INTO paliad.event_categories
    (slug, parent_id, label_de, label_en, sort_order, is_leaf, is_active, forums)
VALUES
    ('cms-eingang.gegenseite.de-bgh-inf.nzb',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite.de-bgh-inf'),
     'Nichtzulassungsbeschwerde', 'Non-Admission Appeal (NZB)', 100, true, true, ARRAY['de']),
    ('cms-eingang.gegenseite.de-bgh-inf.nzb-begr',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite.de-bgh-inf'),
     'NZB-Begründung', 'NZB Statement of Grounds', 200, true, true, ARRAY['de']),
    ('cms-eingang.gegenseite.de-bgh-inf.revision',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite.de-bgh-inf'),
     'Revisionsschrift', 'Notice of Revision', 300, true, true, ARRAY['de']),
    ('cms-eingang.gegenseite.de-bgh-inf.revisionsbegr',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite.de-bgh-inf'),
     'Revisionsbegründung', 'Revision Statement of Grounds', 400, true, true, ARRAY['de']),
    ('cms-eingang.gegenseite.de-bgh-inf.revisionserw',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite.de-bgh-inf'),
     'Revisionserwiderung', 'Response to Revision', 500, true, true, ARRAY['de'])
ON CONFLICT (slug) DO UPDATE SET
    parent_id  = EXCLUDED.parent_id,
    label_de   = EXCLUDED.label_de,
    label_en   = EXCLUDED.label_en,
    sort_order = EXCLUDED.sort_order,
    is_leaf    = EXCLUDED.is_leaf,
    is_active  = EXCLUDED.is_active,
    forums     = EXCLUDED.forums,
    updated_at = now();

-- ============================================================================
-- 5. DE_NULL_BGH children
-- ============================================================================

INSERT INTO paliad.event_categories
    (slug, parent_id, label_de, label_en, sort_order, is_leaf, is_active, forums)
VALUES
    ('cms-eingang.gegenseite.de-bgh-null.berufung',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite.de-bgh-null'),
     'Berufungsschrift', 'Notice of Appeal', 100, true, true, ARRAY['de']),
    ('cms-eingang.gegenseite.de-bgh-null.begruendung',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite.de-bgh-null'),
     'Berufungsbegründung', 'Statement of Grounds of Appeal', 200, true, true, ARRAY['de']),
    ('cms-eingang.gegenseite.de-bgh-null.erwiderung',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite.de-bgh-null'),
     'Berufungserwiderung', 'Response to Appeal', 300, true, true, ARRAY['de'])
ON CONFLICT (slug) DO UPDATE SET
    parent_id  = EXCLUDED.parent_id,
    label_de   = EXCLUDED.label_de,
    label_en   = EXCLUDED.label_en,
    sort_order = EXCLUDED.sort_order,
    is_leaf    = EXCLUDED.is_leaf,
    is_active  = EXCLUDED.is_active,
    forums     = EXCLUDED.forums,
    updated_at = now();

-- ============================================================================
-- 6. DPMA_BGH_RB children
-- ============================================================================

INSERT INTO paliad.event_categories
    (slug, parent_id, label_de, label_en, sort_order, is_leaf, is_active, forums)
VALUES
    ('cms-eingang.gegenseite.dpma-bgh.rechtsbeschwerde',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite.dpma-bgh'),
     'Rechtsbeschwerde', 'Rechtsbeschwerde', 100, true, true, ARRAY['dpma']),
    ('cms-eingang.gegenseite.dpma-bgh.begruendung',
     (SELECT id FROM paliad.event_categories WHERE slug = 'cms-eingang.gegenseite.dpma-bgh'),
     'Rechtsbeschwerde-Begründung', 'Rechtsbeschwerde Statement of Grounds', 200, true, true, ARRAY['dpma'])
ON CONFLICT (slug) DO UPDATE SET
    parent_id  = EXCLUDED.parent_id,
    label_de   = EXCLUDED.label_de,
    label_en   = EXCLUDED.label_en,
    sort_order = EXCLUDED.sort_order,
    is_leaf    = EXCLUDED.is_leaf,
    is_active  = EXCLUDED.is_active,
    forums     = EXCLUDED.forums,
    updated_at = now();

-- ============================================================================
-- 7. Junctions: leaves → deadline_concepts (with proceeding_type_code
-- narrowing so each result card pills only the relevant proceeding).
-- ============================================================================

INSERT INTO paliad.event_category_concepts (event_category_id, concept_id, proceeding_type_code, sort_order)
SELECT ec.id, dc.id, mapping.proc, 100
  FROM (VALUES
    ('cms-eingang.gegenseite.upc-app.berufungsschrift',           'notice-of-appeal',                      'UPC_APP'),
    ('cms-eingang.gegenseite.upc-app.berufungsbegruendung',       'statement-of-grounds-of-appeal',        'UPC_APP'),
    ('cms-eingang.gegenseite.upc-app.berufungserwiderung',        'response-to-appeal',                    'UPC_APP'),
    ('cms-eingang.gegenseite.upc-app.anschlussberufung',          'cross-appeal',                          'UPC_APP'),
    ('cms-eingang.gegenseite.upc-app.reply-to-cross-appeal',      'reply-to-cross-appeal',                 'UPC_APP'),
    ('cms-eingang.gegenseite.upc-pi.antrag',                      'application-for-provisional-measures',  'UPC_PI'),
    ('cms-eingang.gegenseite.upc-pi.erwiderung',                  'statement-of-defence',                  'UPC_PI'),
    ('cms-eingang.gegenseite.de-bgh-inf.nzb',                     'nichtzulassungsbeschwerde',             'DE_INF_BGH'),
    ('cms-eingang.gegenseite.de-bgh-inf.nzb-begr',                'nichtzulassungsbeschwerde-begruendung', 'DE_INF_BGH'),
    ('cms-eingang.gegenseite.de-bgh-inf.revision',                'revisionsfrist',                        'DE_INF_BGH'),
    ('cms-eingang.gegenseite.de-bgh-inf.revisionsbegr',           'revisionsbegruendung',                  'DE_INF_BGH'),
    ('cms-eingang.gegenseite.de-bgh-inf.revisionserw',            'response-to-appeal',                    'DE_INF_BGH'),
    ('cms-eingang.gegenseite.de-bgh-null.berufung',               'notice-of-appeal',                      'DE_NULL_BGH'),
    ('cms-eingang.gegenseite.de-bgh-null.begruendung',            'statement-of-grounds-of-appeal',        'DE_NULL_BGH'),
    ('cms-eingang.gegenseite.de-bgh-null.erwiderung',             'response-to-appeal',                    'DE_NULL_BGH'),
    ('cms-eingang.gegenseite.dpma-bgh.rechtsbeschwerde',          'rechtsbeschwerde',                      'DPMA_BGH_RB'),
    ('cms-eingang.gegenseite.dpma-bgh.begruendung',               'rechtsbeschwerde-begruendung',          'DPMA_BGH_RB')
  ) AS mapping(leaf_slug, concept_slug, proc)
  JOIN paliad.event_categories ec  ON ec.slug  = mapping.leaf_slug
  JOIN paliad.deadline_concepts dc ON dc.slug  = mapping.concept_slug
ON CONFLICT DO NOTHING;
