-- t-paliad-207 — make the Nichtigkeitswiderklage (CCR) visible in the
-- calculator output when the `with_ccr` flag is set. m's observation
-- 2026-05-18 (interactive session): toggling "Mit Nichtigkeitswider-
-- klage" surfaces the response rules (def_to_ccr, reply, rejoin, …)
-- but the triggering event itself — the act of filing the CCR — is
-- invisible. Per R.25 VerfO the CCR is filed AS PART OF the Statement
-- of Defence with the same 3-month deadline, so the corpus author
-- (mig 028) skipped it. UX is the problem: users see consequences
-- without the cause.
--
-- Net effect: a new `upc.inf.cfi.ccr` row with priority='informational'
-- renders the CCR as a notice card on the timeline (no save action,
-- no extra deadline-to-track; the SoD's deadline already covers it).
-- Date is identical to the SoD (3 months from SoC, same anchor +
-- duration). condition_expr={"flag":"with_ccr"} so the row only appears
-- when the user has flagged that a CCR is being filed.
--
-- Sequence reshuffle: inserting at sequence_order=11 pushes
-- def_to_ccr 11→12 and app_to_amend 12→13 so the timeline reads
-- SoD → CCR → def_to_ccr → app_to_amend (cause before effect). The
-- two UPDATEs are guarded by the SOURCE values so re-apply is a no-op.
--
-- audit_reason set_config required at the top — the deadline_rules
-- audit trigger raises EXCEPTION 'audit reason required' on any
-- mutation without it (cf. mig 099 hotfix history).
--
-- Idempotency:
--   * INSERT uses NOT EXISTS keyed on (proceeding_type_id,
--     submission_code, lifecycle_state='published').
--   * UPDATEs are guarded by current sequence_order value.

SELECT set_config(
    'paliad.audit_reason',
    'mig 100: add upc.inf.cfi.ccr informational rule so CCR filing event is visible when with_ccr flag is set (m''s 2026-05-18 ask, t-paliad-207 interactive session)',
    true);

INSERT INTO paliad.deadline_rules
    (proceeding_type_id, parent_id, submission_code, name, name_en,
     description, primary_party, event_type,
     duration_value, duration_unit, timing,
     rule_code, deadline_notes, deadline_notes_en, sequence_order,
     is_spawn, spawn_proceeding_type_id, spawn_label,
     is_active, legal_source, is_bilateral,
     condition_expr, priority, is_court_set, lifecycle_state)
SELECT
    8,
    (SELECT id FROM paliad.deadline_rules
      WHERE submission_code = 'upc.inf.cfi.soc'
        AND proceeding_type_id = 8
        AND lifecycle_state = 'published'
        AND is_active = true),
    'upc.inf.cfi.ccr',
    'Nichtigkeitswiderklage',
    'Counterclaim for Revocation',
    'Widerklage des Beklagten auf Nichtigkeit des Klagepatents. Wird gemeinsam mit der Klageerwiderung (Statement of Defence) eingereicht (R.25 VerfO); selbe Frist von 3 Monaten ab Zustellung der Klage. Eigener adversarialer Schriftsatz, der die Folge-Schriftsätze (Erwiderung auf Nichtigkeitswiderklage, Replik, Duplik) auslöst.',
    'defendant',
    'filing',
    3,
    'months',
    'after',
    'RoP.025',
    'Wird mit der Klageerwiderung eingereicht (R.25 VerfO); kein separater Fristtermin — selbes Datum wie die Klageerwiderung. Wird informativ angezeigt, damit der auslösende Schriftsatz für die Folgefristen sichtbar bleibt.',
    'Filed together with the Statement of Defence (RoP 25); no separate deadline — same date as the SoD. Surfaced informationally so the triggering submission for the downstream deadlines is visible.',
    11,
    false,
    NULL,
    NULL,
    true,
    'UPC.RoP.25.1',
    false,
    '{"flag":"with_ccr"}'::jsonb,
    'informational',
    false,
    'published'
WHERE NOT EXISTS (
    SELECT 1 FROM paliad.deadline_rules
     WHERE submission_code = 'upc.inf.cfi.ccr'
       AND proceeding_type_id = 8
       AND lifecycle_state = 'published');

-- Sequence reshuffle: bump def_to_ccr and app_to_amend by 1 so the
-- new ccr row at 11 sits between SoD (10) and def_to_ccr. Guarded by
-- the source values to keep idempotency.

UPDATE paliad.deadline_rules
   SET sequence_order = 12
 WHERE submission_code = 'upc.inf.cfi.def_to_ccr'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 11;

UPDATE paliad.deadline_rules
   SET sequence_order = 13
 WHERE submission_code = 'upc.inf.cfi.app_to_amend'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 12;
