-- t-paliad-131 Phase B4: DPMA proceeding types.
--
-- Three new types covering the DPMA → BPatG → BGH chain:
--
--   DPMA_OPP                 Einspruchsverfahren beim DPMA (PatG §59 ff.)
--   DPMA_BPATG_BESCHWERDE    Beschwerde gegen DPMA-Entscheidungen (PatG §73 ff.)
--   DPMA_BGH_RB              Rechtsbeschwerde beim BGH (PatG §100 ff.)
--
-- Naming note: head's PR brief named the third type
-- "DPMA_BPATG_NICHTIGKEIT" but Nichtigkeitsklage lives at BPatG already
-- via DE_NULL (a Nichtigkeitsklage is filed *directly* at BPatG, not
-- chained off DPMA). The natural BGH endpoint of the DPMA chain is the
-- Rechtsbeschwerde per §100/§102 PatG. Using DPMA_BGH_RB; if head meant
-- something else, can rename without data churn.
--
-- Rule sources:
--   PatG §59(1)  — Einspruchsfrist DPMA: 9 Monate ab Veröffentlichung der Erteilung
--   PatG §59(3)  — Erwiderung Patentinhaber: vom DPMA gesetzte Frist (typ. 4 Monate)
--   PatG §73(2)  — Beschwerde BPatG: 1 Monat ab Zustellung der DPMA-Entscheidung
--   PatG §75(1)  — Begründung der Beschwerde: 1 Monat ab Einreichung
--                  der Beschwerde (verlängerbar auf Antrag)
--   PatG §100(1) — Rechtsbeschwerde BGH: 1 Monat ab Zustellung der BPatG-Entscheidung
--   PatG §102(2)/(3) — Begründung der Rechtsbeschwerde: 1 Monat ab Einlegung

-- ============================================================================
-- 1. New proceeding types
-- ============================================================================

INSERT INTO paliad.proceeding_types (code, name, name_en, description, jurisdiction, category, default_color, sort_order, is_active)
VALUES
    ('DPMA_OPP',              'Einspruch DPMA',              'Opposition DPMA',
        'Einspruchsverfahren vor dem Deutschen Patent- und Markenamt (§ 59 ff. PatG). Trigger ist die Veröffentlichung der Patenterteilung im DPMA-Patentblatt.',
        'DPMA', 'fristenrechner', '#1d4ed8', 310, true),
    ('DPMA_BPATG_BESCHWERDE', 'Beschwerde BPatG (DPMA)',     'Appeal BPatG (against DPMA Decision)',
        'Beschwerdeverfahren beim BPatG gegen Entscheidungen des DPMA (§ 73 ff. PatG) — typisch nach abgeschlossenem Einspruch oder Patentprüfung.',
        'DPMA', 'fristenrechner', '#1d4ed8', 320, true),
    ('DPMA_BGH_RB',           'Rechtsbeschwerde BGH',        'Legal Appeal BGH',
        'Rechtsbeschwerde beim BGH gegen Entscheidungen des BPatG, beschränkt auf Rechtsfragen (§ 100 ff. PatG).',
        'DPMA', 'fristenrechner', '#1d4ed8', 330, true)
ON CONFLICT (code) DO UPDATE SET
    name           = EXCLUDED.name,
    name_en        = EXCLUDED.name_en,
    description    = EXCLUDED.description,
    jurisdiction   = EXCLUDED.jurisdiction,
    category       = EXCLUDED.category,
    default_color  = EXCLUDED.default_color,
    sort_order     = EXCLUDED.sort_order,
    is_active      = EXCLUDED.is_active;

-- ============================================================================
-- 2. New DE-only concepts (Rechtsbeschwerde + Begründung)
-- ============================================================================
--
-- Other rules reuse existing shared concepts:
--   - publication                  → dpma_opp.publish (anchor)
--   - opposition                   → dpma_opp.einspruch
--   - statement-of-defence         → dpma_opp.erwiderung
--   - decision                     → DPMA / BPatG / BGH terminal entscheidungen
--   - notice-of-appeal             → dpma_bpatg.beschwerde (Beschwerde IS legally an appeal)
--   - statement-of-grounds-of-appeal → dpma_bpatg.begruendung
--   - oral-hearing                 → dpma_bpatg.termin

INSERT INTO paliad.deadline_concepts (slug, name_de, name_en, description, aliases, party, category, sort_order) VALUES
    ('rechtsbeschwerde',                'Rechtsbeschwerde',                 'Legal Appeal (BGH)',
        'Rechtsbeschwerde gegen Entscheidungen des BPatG zum BGH — beschränkt auf Rechtsfragen (§ 100 ff. PatG). 1-Monats-Frist ab Zustellung der BPatG-Entscheidung.',
        ARRAY['Rechtsbeschwerde', 'legal appeal', 'BGH legal appeal', 'Rechtsbeschwerde BGH'],
        'both', 'submission', 41),
    ('rechtsbeschwerde-begruendung',    'Rechtsbeschwerdebegründung',       'Statement of Grounds for Legal Appeal',
        'Begründung der Rechtsbeschwerde (§ 102(2)/(3) PatG i.V.m. ZPO §551) — 1 Monat ab Einlegung der Rechtsbeschwerde.',
        ARRAY['Rechtsbeschwerdebegründung', 'Begründung Rechtsbeschwerde'],
        'both', 'submission', 42)
ON CONFLICT (slug) DO UPDATE SET
    name_de       = EXCLUDED.name_de,
    name_en       = EXCLUDED.name_en,
    description   = EXCLUDED.description,
    aliases       = EXCLUDED.aliases,
    party         = EXCLUDED.party,
    sort_order    = EXCLUDED.sort_order,
    updated_at    = now();

-- ============================================================================
-- 3. DPMA_OPP rules (anchor = Veröffentlichung der Erteilung)
-- ============================================================================

INSERT INTO paliad.deadline_rules (
    proceeding_type_id, parent_id, code, name, name_en, primary_party, event_type,
    is_mandatory, duration_value, duration_unit, timing, rule_code, deadline_notes, deadline_notes_en,
    sequence_order, condition_flag, concept_id, legal_source, is_spawn, is_active
)
SELECT
    pt.id, NULL,
    'dpma_opp.publish', 'Veröffentlichung der Erteilung', 'Publication of Grant',
    'both', 'filing', true, 0, 'months', 'after', NULL,
    'Veröffentlichung der Patenterteilung im DPMA-Patentblatt — Trigger für die Einspruchsfrist.',
    'Publication of the patent grant in the DPMA gazette — trigger for the opposition period.',
    0, NULL,
    (SELECT id FROM paliad.deadline_concepts WHERE slug = 'publication'),
    NULL, false, true
FROM paliad.proceeding_types pt
WHERE pt.code = 'DPMA_OPP';

INSERT INTO paliad.deadline_rules (
    proceeding_type_id, parent_id, code, name, name_en, primary_party, event_type,
    is_mandatory, duration_value, duration_unit, timing, rule_code, deadline_notes, deadline_notes_en,
    sequence_order, condition_flag, concept_id, legal_source, is_spawn, is_active
)
SELECT
    pt.id, parent_rule.id, new.code, new.name, new.name_en, new.primary_party, new.event_type,
    true, new.duration_value, new.duration_unit, 'after', new.rule_code, new.deadline_notes, new.deadline_notes_en,
    new.sequence_order, NULL,
    (SELECT id FROM paliad.deadline_concepts WHERE slug = new.concept_slug),
    new.legal_source, false, true
FROM (VALUES
    ('dpma_opp.publish', 'dpma_opp.einspruch',    'Einspruchsfrist',                        'Opposition Period',
        'both',      'filing',  9, 'months', '§ 59 PatG',
        'Einspruch gegen ein erteiltes Patent muss binnen 9 Monaten nach Veröffentlichung der Erteilung beim DPMA eingelegt werden (§ 59(1) PatG).',
        'Opposition against a granted patent must be filed at the DPMA within 9 months of publication of grant (PatG §59(1)).',
        10, 'opposition',          'DE.PatG.59.1'),
    ('dpma_opp.einspruch','dpma_opp.erwiderung',  'Erwiderung des Patentinhabers',          'Proprietor''s Response to Opposition',
        'defendant', 'filing',  4, 'months', '§ 59 PatG',
        'Erwiderung des Patentinhabers auf den Einspruch — vom DPMA gesetzte Frist (§ 59(3) PatG), typischerweise ~4 Monate.',
        'Patent proprietor''s response to the opposition — DPMA-set deadline (PatG §59(3)), typically ~4 months.',
        20, 'statement-of-defence','DE.PatG.59.3'),
    ('dpma_opp.publish', 'dpma_opp.entscheidung', 'DPMA-Entscheidung',                      'DPMA Decision',
        'court',     'decision', 0, 'months', NULL, NULL, NULL, 50, 'decision', NULL)
) AS new(parent_code, code, name, name_en, primary_party, event_type, duration_value, duration_unit, rule_code, deadline_notes, deadline_notes_en, sequence_order, concept_slug, legal_source)
JOIN paliad.proceeding_types pt ON pt.code = 'DPMA_OPP'
JOIN paliad.deadline_rules    parent_rule ON parent_rule.proceeding_type_id = pt.id AND parent_rule.code = new.parent_code;

-- ============================================================================
-- 4. DPMA_BPATG_BESCHWERDE rules (anchor = DPMA-Entscheidung-Zustellung)
-- ============================================================================

INSERT INTO paliad.deadline_rules (
    proceeding_type_id, parent_id, code, name, name_en, primary_party, event_type,
    is_mandatory, duration_value, duration_unit, timing, rule_code, deadline_notes, deadline_notes_en,
    sequence_order, condition_flag, concept_id, legal_source, is_spawn, is_active
)
SELECT
    pt.id, NULL,
    'dpma_bpatg.entscheidung', 'Zustellung DPMA-Entscheidung', 'Service of DPMA Decision',
    'both', 'filing', true, 0, 'months', 'after', NULL,
    'Zustellung der DPMA-Entscheidung (Trigger für die Beschwerdefrist).',
    'Service of the DPMA decision (trigger for the appeal period).',
    0, NULL, NULL, NULL, false, true
FROM paliad.proceeding_types pt
WHERE pt.code = 'DPMA_BPATG_BESCHWERDE';

-- Wave 1: Beschwerde anchored on DPMA-Entscheidung
INSERT INTO paliad.deadline_rules (
    proceeding_type_id, parent_id, code, name, name_en, primary_party, event_type,
    is_mandatory, duration_value, duration_unit, timing, rule_code, deadline_notes, deadline_notes_en,
    sequence_order, condition_flag, concept_id, legal_source, is_spawn, is_active
)
SELECT
    pt.id, parent_rule.id,
    'dpma_bpatg.beschwerde', 'Beschwerde', 'Notice of Appeal',
    'both', 'filing', true, 1, 'months', 'after', '§ 73 PatG',
    'Beschwerde beim BPatG binnen 1 Monat nach Zustellung der DPMA-Entscheidung schriftlich einzulegen (§ 73(2) PatG).',
    'Notice of appeal must be filed at the BPatG in writing within 1 month of service of the DPMA decision (PatG §73(2)).',
    10, NULL,
    (SELECT id FROM paliad.deadline_concepts WHERE slug = 'notice-of-appeal'),
    'DE.PatG.73.2', false, true
FROM paliad.proceeding_types pt
JOIN paliad.deadline_rules parent_rule ON parent_rule.proceeding_type_id = pt.id AND parent_rule.code = 'dpma_bpatg.entscheidung'
WHERE pt.code = 'DPMA_BPATG_BESCHWERDE';

-- Wave 2: Begründung anchored on Beschwerde
INSERT INTO paliad.deadline_rules (
    proceeding_type_id, parent_id, code, name, name_en, primary_party, event_type,
    is_mandatory, duration_value, duration_unit, timing, rule_code, deadline_notes, deadline_notes_en,
    sequence_order, condition_flag, concept_id, legal_source, is_spawn, is_active
)
SELECT
    pt.id, parent_rule.id,
    'dpma_bpatg.begruendung', 'Beschwerdebegründung', 'Statement of Grounds of Appeal',
    'both', 'filing', true, 1, 'months', 'after', '§ 75 PatG',
    'Begründung der Beschwerde binnen 1 Monat ab Einreichung der Beschwerde (§ 75(1) PatG). Verlängerung auf Antrag möglich.',
    'Statement of grounds of appeal within 1 month of filing the notice of appeal (PatG §75(1)). Extension possible on request.',
    20, NULL,
    (SELECT id FROM paliad.deadline_concepts WHERE slug = 'statement-of-grounds-of-appeal'),
    'DE.PatG.75.1', false, true
FROM paliad.proceeding_types pt
JOIN paliad.deadline_rules parent_rule ON parent_rule.proceeding_type_id = pt.id AND parent_rule.code = 'dpma_bpatg.beschwerde'
WHERE pt.code = 'DPMA_BPATG_BESCHWERDE';

-- Terminal: mündl. + BPatG-Entscheidung anchored on entscheidung (anchor)
INSERT INTO paliad.deadline_rules (
    proceeding_type_id, parent_id, code, name, name_en, primary_party, event_type,
    is_mandatory, duration_value, duration_unit, timing, rule_code, deadline_notes, deadline_notes_en,
    sequence_order, condition_flag, concept_id, legal_source, is_spawn, is_active
)
SELECT
    pt.id, parent_rule.id, new.code, new.name, new.name_en, new.primary_party, new.event_type,
    true, new.duration_value, new.duration_unit, 'after', new.rule_code, new.deadline_notes, new.deadline_notes_en,
    new.sequence_order, NULL,
    (SELECT id FROM paliad.deadline_concepts WHERE slug = new.concept_slug),
    new.legal_source, false, true
FROM (VALUES
    ('dpma_bpatg.entscheidung', 'dpma_bpatg.termin',       'Mündliche Verhandlung BPatG', 'BPatG Oral Hearing',
        'court', 'hearing',  0, 'months', NULL, NULL, NULL, 50, 'oral-hearing', NULL),
    ('dpma_bpatg.entscheidung', 'dpma_bpatg.entsch_bpatg', 'BPatG-Entscheidung',          'BPatG Decision',
        'court', 'decision', 0, 'months', NULL, NULL, NULL, 60, 'decision',     NULL)
) AS new(parent_code, code, name, name_en, primary_party, event_type, duration_value, duration_unit, rule_code, deadline_notes, deadline_notes_en, sequence_order, concept_slug, legal_source)
JOIN paliad.proceeding_types pt ON pt.code = 'DPMA_BPATG_BESCHWERDE'
JOIN paliad.deadline_rules    parent_rule ON parent_rule.proceeding_type_id = pt.id AND parent_rule.code = new.parent_code;

-- ============================================================================
-- 5. DPMA_BGH_RB rules (anchor = BPatG-Entscheidung-Zustellung)
-- ============================================================================

INSERT INTO paliad.deadline_rules (
    proceeding_type_id, parent_id, code, name, name_en, primary_party, event_type,
    is_mandatory, duration_value, duration_unit, timing, rule_code, deadline_notes, deadline_notes_en,
    sequence_order, condition_flag, concept_id, legal_source, is_spawn, is_active
)
SELECT
    pt.id, NULL,
    'dpma_bgh.entsch_bpatg', 'Zustellung BPatG-Entscheidung', 'Service of BPatG Decision',
    'both', 'filing', true, 0, 'months', 'after', NULL,
    'Zustellung der BPatG-Entscheidung (Trigger für die Rechtsbeschwerdefristen).',
    'Service of the BPatG decision (trigger for the legal-appeal deadlines).',
    0, NULL, NULL, NULL, false, true
FROM paliad.proceeding_types pt
WHERE pt.code = 'DPMA_BGH_RB';

-- Rechtsbeschwerde anchored on entsch_bpatg
INSERT INTO paliad.deadline_rules (
    proceeding_type_id, parent_id, code, name, name_en, primary_party, event_type,
    is_mandatory, duration_value, duration_unit, timing, rule_code, deadline_notes, deadline_notes_en,
    sequence_order, condition_flag, concept_id, legal_source, is_spawn, is_active
)
SELECT
    pt.id, parent_rule.id,
    'dpma_bgh.rechtsbeschwerde', 'Rechtsbeschwerde', 'Legal Appeal',
    'both', 'filing', true, 1, 'months', 'after', '§ 100 PatG',
    'Rechtsbeschwerde gegen die BPatG-Entscheidung beim BGH binnen 1 Monat nach Zustellung der Entscheidung einlegen (§ 100(1) PatG). Beschränkt auf Rechtsfragen.',
    'Legal appeal against the BPatG decision must be filed at the BGH within 1 month of service (PatG §100(1)). Limited to legal questions.',
    10, NULL,
    (SELECT id FROM paliad.deadline_concepts WHERE slug = 'rechtsbeschwerde'),
    'DE.PatG.100.1', false, true
FROM paliad.proceeding_types pt
JOIN paliad.deadline_rules parent_rule ON parent_rule.proceeding_type_id = pt.id AND parent_rule.code = 'dpma_bgh.entsch_bpatg'
WHERE pt.code = 'DPMA_BGH_RB';

-- Begründung anchored on Rechtsbeschwerde
INSERT INTO paliad.deadline_rules (
    proceeding_type_id, parent_id, code, name, name_en, primary_party, event_type,
    is_mandatory, duration_value, duration_unit, timing, rule_code, deadline_notes, deadline_notes_en,
    sequence_order, condition_flag, concept_id, legal_source, is_spawn, is_active
)
SELECT
    pt.id, parent_rule.id,
    'dpma_bgh.begruendung', 'Rechtsbeschwerdebegründung', 'Statement of Grounds for Legal Appeal',
    'both', 'filing', true, 1, 'months', 'after', '§ 102 PatG',
    'Begründung der Rechtsbeschwerde binnen 1 Monat ab Einlegung (§ 102(2)/(3) PatG i.V.m. § 551 ZPO).',
    'Statement of grounds for the legal appeal within 1 month of filing the appeal (PatG §102(2)/(3) read with ZPO §551).',
    20, NULL,
    (SELECT id FROM paliad.deadline_concepts WHERE slug = 'rechtsbeschwerde-begruendung'),
    'DE.PatG.102', false, true
FROM paliad.proceeding_types pt
JOIN paliad.deadline_rules parent_rule ON parent_rule.proceeding_type_id = pt.id AND parent_rule.code = 'dpma_bgh.rechtsbeschwerde'
WHERE pt.code = 'DPMA_BGH_RB';

-- BGH-Entscheidung
INSERT INTO paliad.deadline_rules (
    proceeding_type_id, parent_id, code, name, name_en, primary_party, event_type,
    is_mandatory, duration_value, duration_unit, timing, rule_code, deadline_notes, deadline_notes_en,
    sequence_order, condition_flag, concept_id, legal_source, is_spawn, is_active
)
SELECT
    pt.id, parent_rule.id,
    'dpma_bgh.entsch_bgh', 'BGH-Entscheidung', 'BGH Decision',
    'court', 'decision', true, 0, 'months', 'after', NULL, NULL, NULL,
    50, NULL,
    (SELECT id FROM paliad.deadline_concepts WHERE slug = 'decision'),
    NULL, false, true
FROM paliad.proceeding_types pt
JOIN paliad.deadline_rules parent_rule ON parent_rule.proceeding_type_id = pt.id AND parent_rule.code = 'dpma_bgh.entsch_bpatg'
WHERE pt.code = 'DPMA_BGH_RB';
