-- t-paliad-264 / m/paliad#95 — Fix de.inf.lg Replik + Duplik sequencing.
--
-- BEFORE this migration, the de.inf.lg rules for Replik and Duplik
-- had parent_id = NULL with duration_value = 4 weeks each. The
-- projection therefore anchored both off the proceeding's trigger
-- date (Klageerhebung) and added 4 weeks → both rows rendered at the
-- same calendar date, BEFORE Klageerwiderung (which sits at
-- Klageerhebung + 6 weeks per § 276 Abs. 1 S. 2 ZPO).
--
-- Correct ZPO sequence for first-instance infringement before the
-- Landgericht is:
--
--   Klageerhebung (§ 253 ZPO)
--     → Anzeige der Verteidigungsbereitschaft (§ 276 Abs. 1 S. 1 ZPO,
--       2 Wochen ab Zustellung der Klage)
--     → Klageerwiderung (§ 276 Abs. 1 S. 2 + § 277 ZPO; vom Gericht
--       gesetzte Frist von mindestens 2 Wochen, in der Praxis 6 Wochen)
--     → Replik (vom Gericht gesetzte Frist; Anordnungskompetenz aus
--       § 273 ZPO, prozessuale Förderungspflicht der Parteien aus
--       § 282 ZPO; in der Praxis ~ 4 Wochen ab Zustellung der
--       Klageerwiderung)
--     → Duplik (vom Gericht gesetzte Frist; § 273, § 282 ZPO; in der
--       Praxis ~ 4 Wochen ab Zustellung der Replik)
--
-- Replik and Duplik have NO statutory period — the Landgericht fixes
-- the period in its prozessleitende Verfügung. We model them as
-- is_court_set = true with a placeholder 4-week duration anchored on
-- the immediately preceding filing so the timeline (a) renders them
-- in strict chronological order and (b) gives the lawyer a sane
-- notional date that can be overridden via "Datum setzen" once the
-- court issues the actual period.
--
-- legal_source set to DE.ZPO.273 (Vorbereitung des Termins —
-- court's case-management power that authorises setting Replik /
-- Duplik periods). The full citation chain (§§ 273, 282 ZPO) lives
-- in deadline_notes so the rendered card explains the source.
--
-- Scope strictly de.inf.lg / cfi per the t-paliad-264 brief. Other
-- jurisdictions are out of scope and will be addressed via curie's
-- m/paliad#94 audit follow-ups (Wave 0+).
--
-- Slot note: this migration originally landed as 123 in an earlier
-- iteration; cronus's t-paliad-246 Backup-Mode migration won slot
-- 123 in parallel-merge order, so this one shifted to 124.
--
-- Idempotency: each UPDATE is guarded by a WHERE clause that only
-- matches the pre-fix row state (parent_id IS NULL on Replik /
-- Duplik, since that was the load-bearing bug). A re-apply against
-- a DB that already carries the fix matches zero rows and no-ops —
-- no duplicate audit-log rows in paliad.deadline_rule_audit, no
-- redundant writes. Mig 095 convention.
--
-- audit_reason set_config required at the top — the mig 079 trigger
-- on paliad.deadline_rules raises EXCEPTION 'audit reason required'
-- on any UPDATE without it.

SELECT set_config(
    'paliad.audit_reason',
    'mig 124: t-paliad-264 / m/paliad#95 — anchor de.inf.lg Replik on Klageerwiderung and Duplik on Replik, mark both is_court_set per § 273 ZPO',
    true);

-- Replik anchors on Klageerwiderung (de.inf.lg.erwidg).
-- Guard: parent_id IS NULL — only fires against the pre-fix shape.
UPDATE paliad.deadline_rules
SET parent_id = (
        SELECT id FROM paliad.deadline_rules
        WHERE submission_code = 'de.inf.lg.erwidg'
          AND is_active = true
        LIMIT 1
    ),
    is_court_set   = true,
    legal_source   = 'DE.ZPO.273',
    deadline_notes = 'Frist vom Gericht in der prozessleitenden Verfügung bestimmt (§ 273 ZPO, prozessuale Förderungspflicht § 282 ZPO). In der Praxis ca. 4 Wochen ab Zustellung der Klageerwiderung; mit "Datum setzen" überschreiben, sobald die gerichtliche Verfügung vorliegt.',
    deadline_notes_en = 'Period set by the court in its case-management order (§ 273 ZPO; parties'' duty to file timely under § 282 ZPO). Typically ca. 4 weeks after service of the Statement of Defence; use "Set date" to override once the court issues the actual period.'
WHERE submission_code = 'de.inf.lg.replik'
  AND is_active = true
  AND parent_id IS NULL;

-- Duplik anchors on Replik (de.inf.lg.replik).
-- Guard: parent_id IS NULL — only fires against the pre-fix shape.
UPDATE paliad.deadline_rules
SET parent_id = (
        SELECT id FROM paliad.deadline_rules
        WHERE submission_code = 'de.inf.lg.replik'
          AND is_active = true
        LIMIT 1
    ),
    is_court_set   = true,
    legal_source   = 'DE.ZPO.273',
    deadline_notes = 'Frist vom Gericht in der prozessleitenden Verfügung bestimmt (§ 273 ZPO, prozessuale Förderungspflicht § 282 ZPO). In der Praxis ca. 4 Wochen ab Zustellung der Replik; mit "Datum setzen" überschreiben, sobald die gerichtliche Verfügung vorliegt.',
    deadline_notes_en = 'Period set by the court in its case-management order (§ 273 ZPO; parties'' duty to file timely under § 282 ZPO). Typically ca. 4 weeks after service of the Reply; use "Set date" to override once the court issues the actual period.'
WHERE submission_code = 'de.inf.lg.duplik'
  AND is_active = true
  AND parent_id IS NULL;
