-- t-paliad-131 Phase B1: UPC counterclaim cross-flows.
--
-- Closes m's primary complaint: today's `with_ccr` flag on UPC_INF only
-- swaps the rejoinder duration from 1mo to 2mo. Per R.29 (UPC RoP) the
-- with-CCR flow ALSO adds 5–7 new submissions across the claimant /
-- defendant exchange:
--   R.29(a) — claimant: Defence to CCR + (opt) Application to amend (R.30)
--   R.29(d) — defendant: Reply to Defence to CCR + (opt) Defence to App to amend
--   R.29(e) — claimant: Rejoinder + (opt) Reply to Defence to amend
--   R.32(1) / R.32(3) — defendant amend cycle: Defence (2mo) / Reply (1mo) / Rejoinder (1mo)
--
-- And on the revocation side (UPC_REV) per R.49(2):
--   (a) Application to amend  → R.55 (= R.32 m.m.) cycle
--   (b) Counterclaim for infringement → R.56 cycle
--
-- Citations verified against data.laws_contents (youpcdb, UPCRoP).
--
-- Two new flags on UPC_INF: nested `with_amend` (only meaningful when
-- with_ccr is also set, since R.30 application is only available with
-- a CCR). UPC_REV gets two parallel independent flags: `with_amend`
-- (R.49.2(a)) and `with_cci` (R.49.2(b)). Both can be on simultaneously.
--
-- New rules are inserted in waves because subsequent rules reference
-- prior-wave inserts as their parent_id (a single INSERT statement can't
-- self-reference its rows in the same SELECT).

-- ============================================================================
-- 1. New concept rows
-- ============================================================================

INSERT INTO paliad.deadline_concepts (slug, name_de, name_en, description, aliases, party, category, sort_order) VALUES
    ('counterclaim-for-revocation',                      'Nichtigkeitswiderklage',                                'Counterclaim for Revocation',                                  'Eingeschlossene Widerklage auf Nichtigerklärung des Patents in der Klageerwiderung (UPC R.25).',                                       ARRAY['Nichtigkeitswiderklage', 'Counterclaim for Revocation', 'CCR', 'Widerklage Nichtigkeit'],                                                                  'defendant', 'submission', 23),
    ('defence-to-counterclaim-for-revocation',           'Erwiderung auf Nichtigkeitswiderklage',                 'Defence to Counterclaim for Revocation',                       'Erwiderung des Klägers auf eine in der Klageerwiderung enthaltene Nichtigkeitswiderklage (R.29.a).',                                  ARRAY['Erwiderung Nichtigkeitswiderklage', 'Defence to CCR', 'Defence to Counterclaim for Revocation'],                                                            'claimant',  'submission', 24),
    ('defence-to-application-to-amend',                  'Erwiderung auf Patentänderungsantrag',                  'Defence to Application to Amend',                              'Erwiderung der Gegenseite auf den Patentänderungsantrag (R.32.1 / R.43.3).',                                                          ARRAY['Erwiderung Patentänderungsantrag', 'Defence to App to amend', 'Defence to Application to Amend'],                                                          'defendant', 'submission', 26),
    ('reply-to-defence-to-counterclaim-for-revocation',  'Replik auf Erwiderung zur Nichtigkeitswiderklage',      'Reply to Defence to Counterclaim for Revocation',              'Replik des Beklagten/Patentinhabers auf die Erwiderung zur Nichtigkeitswiderklage (R.29.d).',                                          ARRAY['Replik Erwiderung CCR', 'Reply to Defence to CCR'],                                                                                                          'defendant', 'submission', 27),
    ('reply-to-defence-to-application-to-amend',         'Replik auf Erwiderung zum Patentänderungsantrag',       'Reply to Defence to Application to Amend',                     'Replik des Patentinhabers auf die Erwiderung zum Patentänderungsantrag (R.32.3).',                                                    ARRAY['Replik Patentänderungsantrag', 'Reply to Defence to amend'],                                                                                                  'claimant',  'submission', 28),
    ('rejoinder-on-reply-to-defence-to-ccr',             'Duplik auf Replik zur Erwiderung Nichtigkeitsw.',       'Rejoinder to Reply to Defence to Counterclaim for Revocation', 'Duplik des Klägers auf die Replik zur Erwiderung der Nichtigkeitswiderklage (R.29.e).',                                                ARRAY['Duplik Replik CCR', 'Rejoinder Reply to Defence to CCR'],                                                                                                    'claimant',  'submission', 29),
    ('rejoinder-on-reply-to-amend',                      'Duplik auf Replik zum Patentänderungsantrag',           'Rejoinder to Reply on Application to Amend',                   'Duplik der Gegenseite auf die Replik zum Patentänderungsantrag (R.32.3).',                                                            ARRAY['Duplik Patentänderungsantrag', 'Rejoinder on amend'],                                                                                                         'defendant', 'submission', 30),
    ('counterclaim-for-infringement',                    'Verletzungswiderklage',                                 'Counterclaim for Infringement',                                'Verletzungswiderklage des Patentinhabers innerhalb des Nichtigkeitsverfahrens, eingeschlossen in der Erwiderung zur Nichtigkeitsklage (R.49.2.b).', ARRAY['Verletzungswiderklage', 'Counterclaim for Infringement', 'CCI'],                                                                                              'defendant', 'submission', 31),
    ('defence-to-counterclaim-for-infringement',         'Erwiderung auf Verletzungswiderklage',                  'Defence to Counterclaim for Infringement',                     'Erwiderung des Klägers im Nichtigkeitsverfahren auf die Verletzungswiderklage (R.56.1).',                                              ARRAY['Erwiderung Verletzungswiderklage', 'Defence to CCI'],                                                                                                        'claimant',  'submission', 32),
    ('reply-to-defence-to-counterclaim-for-infringement','Replik auf Erwiderung zur Verletzungswiderklage',       'Reply to Defence to Counterclaim for Infringement',            'Replik des Patentinhabers auf die Erwiderung zur Verletzungswiderklage (R.56.3).',                                                    ARRAY['Replik Verletzungswiderklage', 'Reply to Defence to CCI'],                                                                                                    'defendant', 'submission', 33),
    ('rejoinder-on-counterclaim-for-infringement',       'Duplik auf Replik zur Erwiderung Verletzungswiderkl.',  'Rejoinder to Reply on Counterclaim for Infringement',          'Duplik des Klägers auf die Replik zur Erwiderung der Verletzungswiderklage (R.56.4).',                                                ARRAY['Duplik Verletzungswiderklage', 'Rejoinder on CCI'],                                                                                                          'claimant',  'submission', 34)
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();

-- ============================================================================
-- 2. Renumber existing UPC_INF / UPC_REV sequence_orders to leave gaps so
--    new rules can interleave chronologically.
-- ============================================================================

WITH proc AS (SELECT id FROM paliad.proceeding_types WHERE code = 'UPC_INF')
UPDATE paliad.deadline_rules dr
   SET sequence_order = CASE dr.code
       WHEN 'inf.soc'      THEN 0
       WHEN 'inf.sod'      THEN 10
       WHEN 'inf.reply'    THEN 20
       WHEN 'inf.rejoin'   THEN 30
       WHEN 'inf.interim'  THEN 40
       WHEN 'inf.oral'     THEN 50
       WHEN 'inf.decision' THEN 60
       WHEN 'inf.cost_app' THEN 70
       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 = 'UPC_REV')
UPDATE paliad.deadline_rules dr
   SET sequence_order = CASE dr.code
       WHEN 'rev.app'      THEN 0
       WHEN 'rev.defence'  THEN 10
       WHEN 'rev.reply'    THEN 20
       WHEN 'rev.rejoin'   THEN 30
       WHEN 'rev.interim'  THEN 50
       WHEN 'rev.oral'     THEN 60
       WHEN 'rev.decision' THEN 70
       ELSE dr.sequence_order
   END
  FROM proc p
 WHERE dr.proceeding_type_id = p.id;

-- ============================================================================
-- 3. UPC_INF cross-flow rules — wave 1 (parents pre-existing inf.sod)
-- ============================================================================

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, new.condition_flag,
    (SELECT id FROM paliad.deadline_concepts WHERE slug = new.concept_slug),
    new.legal_source, false, true
FROM (VALUES
    ('inf.sod',          'inf.def_to_ccr',       'Erwiderung auf Nichtigkeitswiderklage',                'Defence to Counterclaim for Revocation',          'claimant',  'filing', 2, 'months', 'RoP.029.a',
        'Innerhalb von 2 Monaten nach Zustellung der Klageerwiderung mit Nichtigkeitswiderklage. Schließt Replik zur Klageerwiderung und ggf. Patentänderungsantrag (R.30) ein.',
        'Within 2 months of service of the SoD which includes a CCR. Includes the Reply to SoD and (if applicable) the Application to amend per R.30.',
        11, ARRAY['with_ccr']::text[],          'defence-to-counterclaim-for-revocation', 'UPC.RoP.29.a'),
    ('inf.sod',          'inf.app_to_amend',     'Antrag auf Patentänderung',                            'Application to Amend the Patent',                  'claimant',  'filing', 2, 'months', 'RoP.030.1',
        'Optional in der Erwiderung zur Nichtigkeitswiderklage einzureichen — siehe R.30. Spätere Änderungsanträge nur mit Zustimmung des Gerichts (R.30.2).',
        'Optional, lodged together with the Defence to CCR per R.30. Later amendment requests admitted only with leave of the Court (R.30.2).',
        12, ARRAY['with_ccr','with_amend']::text[], 'application-to-amend',                  'UPC.RoP.30.1')
) 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, condition_flag, concept_slug, legal_source)
JOIN paliad.proceeding_types pt ON pt.code = 'UPC_INF'
JOIN paliad.deadline_rules    parent_rule ON parent_rule.proceeding_type_id = pt.id AND parent_rule.code = new.parent_code;

-- ============================================================================
-- 4. UPC_INF cross-flow rules — wave 2 (parents = wave-1 inserts)
-- ============================================================================

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, new.condition_flag,
    (SELECT id FROM paliad.deadline_concepts WHERE slug = new.concept_slug),
    new.legal_source, false, true
FROM (VALUES
    ('inf.app_to_amend', 'inf.def_to_amend',     'Erwiderung auf Patentänderungsantrag',                 'Defence to Application to Amend',                  'defendant', 'filing', 2, 'months', 'RoP.032.1',
        'Innerhalb von 2 Monaten nach Zustellung des Patentänderungsantrags.',
        'Within 2 months of service of the Application to amend the patent.',
        21, ARRAY['with_ccr','with_amend']::text[], 'defence-to-application-to-amend',       'UPC.RoP.32.1'),
    ('inf.def_to_ccr',   'inf.reply_def_ccr',    'Replik auf Erwiderung zur Nichtigkeitswiderklage',     'Reply to Defence to Counterclaim for Revocation',  'defendant', 'filing', 2, 'months', 'RoP.029.d',
        'Innerhalb von 2 Monaten nach Zustellung der Erwiderung zur Nichtigkeitswiderklage. Schließt Duplik zur Replik auf SoD und ggf. Erwiderung zum Patentänderungsantrag ein.',
        'Within 2 months of service of the Defence to CCR. Includes the Rejoinder to Reply to SoD and (if applicable) the Defence to Application to amend per R.32.',
        22, ARRAY['with_ccr']::text[],              'reply-to-defence-to-counterclaim-for-revocation', 'UPC.RoP.29.d')
) 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, condition_flag, concept_slug, legal_source)
JOIN paliad.proceeding_types pt ON pt.code = 'UPC_INF'
JOIN paliad.deadline_rules    parent_rule ON parent_rule.proceeding_type_id = pt.id AND parent_rule.code = new.parent_code;

-- ============================================================================
-- 5. UPC_INF cross-flow rules — wave 3 (parents = wave-2 inserts)
-- ============================================================================

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, new.condition_flag,
    (SELECT id FROM paliad.deadline_concepts WHERE slug = new.concept_slug),
    new.legal_source, false, true
FROM (VALUES
    ('inf.def_to_amend', 'inf.reply_def_amd',    'Replik auf Erwiderung zum Patentänderungsantrag',      'Reply to Defence to Application to Amend',         'claimant',  'filing', 1, 'months', 'RoP.032.3',
        'Innerhalb von 1 Monat nach Zustellung der Erwiderung zum Patentänderungsantrag.',
        'Within 1 month of service of the Defence to the Application to amend the patent.',
        31, ARRAY['with_ccr','with_amend']::text[], 'reply-to-defence-to-application-to-amend', 'UPC.RoP.32.3'),
    ('inf.reply_def_ccr','inf.rejoin_reply_ccr', 'Duplik auf Replik zur Erwiderung Nichtigkeitswiderklage','Rejoinder to Reply to Defence to Counterclaim for Revocation','claimant', 'filing', 1, 'months', 'RoP.029.e',
        'Innerhalb von 1 Monat nach Zustellung der Replik auf Erwiderung zur Nichtigkeitswiderklage. Beschränkt auf Erwiderung der in der Replik aufgeworfenen Punkte.',
        'Within 1 month of service of the Reply to Defence to CCR. Limited to a response to matters raised in the Reply.',
        32, ARRAY['with_ccr']::text[],              'rejoinder-on-reply-to-defence-to-ccr',  'UPC.RoP.29.e')
) 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, condition_flag, concept_slug, legal_source)
JOIN paliad.proceeding_types pt ON pt.code = 'UPC_INF'
JOIN paliad.deadline_rules    parent_rule ON parent_rule.proceeding_type_id = pt.id AND parent_rule.code = new.parent_code;

-- ============================================================================
-- 6. UPC_INF cross-flow rules — wave 4 (final amend rejoinder)
-- ============================================================================

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, 'inf.rejoin_amd', 'Duplik auf Replik zum Patentänderungsantrag', 'Rejoinder to Reply on Application to Amend',
    'defendant', 'filing', true, 1, 'months', 'after', 'RoP.032.3',
    'Innerhalb von 1 Monat nach Zustellung der Replik auf Erwiderung zum Patentänderungsantrag. Beschränkt auf die in der Replik aufgeworfenen Punkte.',
    'Within 1 month of service of the Reply to Defence to Application to amend. Limited to matters raised in the Reply.',
    33, ARRAY['with_ccr','with_amend']::text[],
    (SELECT id FROM paliad.deadline_concepts WHERE slug = 'rejoinder-on-reply-to-amend'),
    'UPC.RoP.32.3', 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 = 'inf.reply_def_amd'
WHERE pt.code = 'UPC_INF';

-- ============================================================================
-- 7. UPC_REV cross-flow rules — wave 1 (parents = pre-existing rev.defence)
-- ============================================================================

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, new.condition_flag,
    (SELECT id FROM paliad.deadline_concepts WHERE slug = new.concept_slug),
    new.legal_source, false, true
FROM (VALUES
    ('rev.defence',      'rev.app_to_amend',     'Antrag auf Patentänderung',                              'Application to Amend the Patent',                            'defendant', 'filing', 0, 'months', 'RoP.049.2.a',
        'Optional in der Erwiderung zur Nichtigkeitsklage einzureichen (R.49.2.a). Reicht der Patentinhaber einen Patentänderungsantrag ein, eröffnet das den R.55 (= R.32 m.m.) Zyklus.',
        'Optional, lodged together with the Defence to revocation per R.49.2(a). Filing this opens the R.55 (= R.32 m.m.) cycle.',
        11, ARRAY['with_amend']::text[],            'application-to-amend',                  'UPC.RoP.49.2.a'),
    ('rev.defence',      'rev.cc_inf',           'Verletzungswiderklage',                                   'Counterclaim for Infringement',                              'defendant', 'filing', 0, 'months', 'RoP.049.2.b',
        'Optional in der Erwiderung zur Nichtigkeitsklage einzuschließen (R.49.2.b, R.50). Eröffnet den R.56-Zyklus.',
        'Optional, included in the Defence to revocation per R.49.2(b) and R.50. Opens the R.56 cycle.',
        12, ARRAY['with_cci']::text[],              'counterclaim-for-infringement',          'UPC.RoP.49.2.b')
) 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, condition_flag, concept_slug, legal_source)
JOIN paliad.proceeding_types pt ON pt.code = 'UPC_REV'
JOIN paliad.deadline_rules    parent_rule ON parent_rule.proceeding_type_id = pt.id AND parent_rule.code = new.parent_code;

-- ============================================================================
-- 8. UPC_REV cross-flow rules — wave 2 (defences to amend / CCI)
-- ============================================================================

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, new.condition_flag,
    (SELECT id FROM paliad.deadline_concepts WHERE slug = new.concept_slug),
    new.legal_source, false, true
FROM (VALUES
    ('rev.app_to_amend', 'rev.def_to_amend',     'Erwiderung auf Patentänderungsantrag',                   'Defence to Application to Amend',                            'claimant',  'filing', 2, 'months', 'RoP.043.3',
        'Innerhalb von 2 Monaten nach Zustellung des Patentänderungsantrags (R.43.3 i.V.m. R.55, R.32.1 m.m.).',
        'Within 2 months of service of the Application to amend (R.43.3 read with R.55 and R.32.1 m.m.).',
        21, ARRAY['with_amend']::text[],            'defence-to-application-to-amend',       'UPC.RoP.43.3'),
    ('rev.cc_inf',       'rev.def_cci',          'Erwiderung auf Verletzungswiderklage',                   'Defence to Counterclaim for Infringement',                   'claimant',  'filing', 2, 'months', 'RoP.056.1',
        'Innerhalb von 2 Monaten nach Zustellung der Verletzungswiderklage (R.56.1).',
        'Within 2 months of service of the Counterclaim for infringement (R.56.1).',
        22, ARRAY['with_cci']::text[],              'defence-to-counterclaim-for-infringement','UPC.RoP.56.1')
) 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, condition_flag, concept_slug, legal_source)
JOIN paliad.proceeding_types pt ON pt.code = 'UPC_REV'
JOIN paliad.deadline_rules    parent_rule ON parent_rule.proceeding_type_id = pt.id AND parent_rule.code = new.parent_code;

-- ============================================================================
-- 9. UPC_REV cross-flow rules — wave 3 (replies)
-- ============================================================================

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, new.condition_flag,
    (SELECT id FROM paliad.deadline_concepts WHERE slug = new.concept_slug),
    new.legal_source, false, true
FROM (VALUES
    ('rev.def_to_amend', 'rev.reply_def_amd',    'Replik auf Erwiderung zum Patentänderungsantrag',        'Reply to Defence to Application to Amend',                   'defendant', 'filing', 1, 'months', 'RoP.032.3',
        'Innerhalb von 1 Monat nach Zustellung der Erwiderung zum Patentänderungsantrag (R.32.3 m.m.).',
        'Within 1 month of service of the Defence to Application to amend (R.32.3 m.m.).',
        31, ARRAY['with_amend']::text[],            'reply-to-defence-to-application-to-amend','UPC.RoP.32.3'),
    ('rev.def_cci',      'rev.reply_def_cci',    'Replik auf Erwiderung zur Verletzungswiderklage',        'Reply to Defence to Counterclaim for Infringement',          'defendant', 'filing', 1, 'months', 'RoP.056.3',
        'Innerhalb von 1 Monat nach Zustellung der Erwiderung zur Verletzungswiderklage (R.56.3).',
        'Within 1 month of service of the Defence to Counterclaim for infringement (R.56.3).',
        32, ARRAY['with_cci']::text[],              'reply-to-defence-to-counterclaim-for-infringement','UPC.RoP.56.3')
) 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, condition_flag, concept_slug, legal_source)
JOIN paliad.proceeding_types pt ON pt.code = 'UPC_REV'
JOIN paliad.deadline_rules    parent_rule ON parent_rule.proceeding_type_id = pt.id AND parent_rule.code = new.parent_code;

-- ============================================================================
-- 10. UPC_REV cross-flow rules — wave 4 (rejoinders)
-- ============================================================================

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, new.condition_flag,
    (SELECT id FROM paliad.deadline_concepts WHERE slug = new.concept_slug),
    new.legal_source, false, true
FROM (VALUES
    ('rev.reply_def_amd','rev.rejoin_amd',       'Duplik auf Replik zum Patentänderungsantrag',            'Rejoinder to Reply on Application to Amend',                 'claimant',  'filing', 1, 'months', 'RoP.032.3',
        'Innerhalb von 1 Monat nach Zustellung der Replik (R.32.3 m.m.). Beschränkt auf die in der Replik aufgeworfenen Punkte.',
        'Within 1 month of service of the Reply (R.32.3 m.m.). Limited to matters raised in the Reply.',
        40, ARRAY['with_amend']::text[],            'rejoinder-on-reply-to-amend',           'UPC.RoP.32.3'),
    ('rev.reply_def_cci','rev.rejoin_cci',       'Duplik auf Replik zur Erwiderung Verletzungswiderklage', 'Rejoinder to Reply on Counterclaim for Infringement',        'claimant',  'filing', 1, 'months', 'RoP.056.4',
        'Innerhalb von 1 Monat nach Zustellung der Replik (R.56.4). Beschränkt auf die in der Replik aufgeworfenen Punkte.',
        'Within 1 month of service of the Reply (R.56.4). Limited to matters raised in the Reply.',
        41, ARRAY['with_cci']::text[],              'rejoinder-on-counterclaim-for-infringement','UPC.RoP.56.4')
) 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, condition_flag, concept_slug, legal_source)
JOIN paliad.proceeding_types pt ON pt.code = 'UPC_REV'
JOIN paliad.deadline_rules    parent_rule ON parent_rule.proceeding_type_id = pt.id AND parent_rule.code = new.parent_code;
