-- t-paliad-210 / legal-citation backfill — apply huygens's HIGH/MED
-- proposals from docs/proposals/legal-citation-backfill-2026-05-18.md
-- (commit 391be09) PLUS m's 2026-05-18 FLAG walk-through (paliadin/head
-- instruction-msg 2002). Scope grew from the original brief: m approved
-- filling almost every category, with only 3 FLAG-J rows left NULL.
--
-- Touches (in 8 buckets, ~135 rows):
--
--   § 1  Easy wins                — 6 rows. rule_code only. The 2
--                                   § 123 PatG twins (Wiedereinsetzung)
--                                   move into the FLAG-A dedup bucket
--                                   below; not filled here.
--
--   § 2  HIGH/MED proceeding-typed — 15 rows. rule_code + legal_source.
--
--   § 3  HIGH/MED orphans         — 47 rows. rule_code + legal_source.
--                                   For UPC rows also rule_codes[]
--                                   normalized to ARRAY[rule_code].
--                                   Excludes 3 archive-dest dup rows
--                                   that are filled via the canonical
--                                   in § 4 instead (5c0508f4 /
--                                   791fd0f7 / d886f46f).
--
--   § 4  FLAG-A dedup (clean only) — 3 canonical fills + 3 archive
--                                   flips. Only sets where the
--                                   duplicate rows share an existing
--                                   rule_codes[] value (or both are
--                                   NULL) are deduped:
--                                     * 2× "Wiedereinsetzungsantrag
--                                       § 123 PatG" — canonical
--                                       b588fa64 (lowest UUID),
--                                       archive c24d494c.
--                                     * 2× "Berufungsschrift R.220.1
--                                       (a)/(b)" — canonical 1dfba5b1
--                                       (filled in § 3.3), archive
--                                       5c0508f4.
--                                     * 2× "Berufungsbegründung R.220.1
--                                       (a)/(b)" — canonical 573df3d1
--                                       (filled in § 3.3), archive
--                                       791fd0f7.
--
--                                   DEFERRED (paliadin/head msg 2006,
--                                   pending m's call): 6× "Mängel-
--                                   beseitigung / Zahlung" and 2×
--                                   "Beginn des Hauptsacheverfahrens".
--                                   Each row in those sets carries a
--                                   DIFFERENT existing rule_codes[]
--                                   value (Mängelbeseitigung: RoP.207
--                                   .6.a, RoP.253.2, RoP.016.3.a,
--                                   RoP.027.2, RoP.089.2, RoP.229.2;
--                                   Beginn-Hauptsache: RoP.198 vs
--                                   RoP.213). These may be distinct
--                                   procedural-context rules masquer-
--                                   ading as duplicates; m owns the
--                                   collapse-or-preserve decision.
--                                   Mig 097 leaves all 8 rows
--                                   untouched (rule_code stays NULL,
--                                   rule_codes[] stays as-is, neither
--                                   archived nor filled).
--
--   § 5  FLAG-B court-scheduled    — 26 rows. Per m: "try to find the
--                                   rules — they often exist." Cites
--                                   the framing norm authorising the
--                                   court to schedule the event (RoP.111
--                                   for UPC oral hearings, RoP.118 for
--                                   UPC decisions, § 285 ZPO / § 300
--                                   ZPO for DE Verhandlung / Urteil,
--                                   § 47 / 78 / 79 / 107 PatG for
--                                   DPMA/BPatG/BGH variants, etc.).
--
--   § 6  FLAG-C/D rubber-stamp     — 5 rows. rev.reply/rev.rejoin/
--                                   app.response use canonical RoP.5x
--                                   regardless of duration-vs-norm
--                                   mismatch (m: "just go ahead").
--                                   de_inf.replik/de_inf.duplik cite
--                                   § 273 ZPO (court-set framing).
--
--   § 7  FLAG-E service triggers   — 6 rows (DE/EPA). Service-trigger
--                                   citations on Zustellung events.
--                                   UPC initial-submission rows carry
--                                   the RoP.271.b 10-day deferral as a
--                                   secondary cite in rule_codes[]
--                                   (handled in § 9 below).
--
--   § 8  FLAG-F combined-pleading  — 5 rows. Use rule_codes[] multi-cite
--                                   array (column already exists from
--                                   mig 095). Primary cite in
--                                   rule_code, full set in rule_codes[].
--
--   § 9  FLAG-G/H/I + RoP.271.b    — 13 rows. G: 2 Patentänderung
--                                   orphans split by INF/REV context.
--                                   H: 8 sub-paragraph spot-checks
--                                   applied as-is per the doc. I: 3
--                                   negative-declaration rows cite
--                                   RoP.069 by analogy.
--                                   Plus: 5 UPC initial-submission rows
--                                   append RoP.271.b to rule_codes[]
--                                   as the 10-day service deferral.
--                                   m flagged this distinct from the
--                                   primary substantive cite.
--
--   § 10 R.19 label rename         — 2 rows max. inf.prelim / rev.prelim:
--                                   set name to "Einspruch (R. 19 VerfO)"
--                                   / "Einspruch (R. 19 i.V.m. R. 46
--                                   VerfO)" + rule_code 'RoP.019.1'.
--                                   Originally drafted in fermi's
--                                   t-paliad-207 session; m applied the
--                                   rename live on prod and asked us to
--                                   consolidate the mig here per Path-A.
--                                   Guard `name LIKE 'Vorab-Einrede%'`
--                                   makes this a defensive no-op on the
--                                   prod DB (fermi already wrote there)
--                                   but applies cleanly on any future
--                                   deploy that hasn't seen the live
--                                   write.
--
--   § 11 Side-fix RoP.49.1 → .049.1 — 1 row. rev.defence carries an
--                                   un-padded rule_code; all other UPC
--                                   RoP rules under 100 use 3-digit
--                                   padding. legal_source stays
--                                   'UPC.RoP.49.1' (structured locator
--                                   never pads).
--
-- FLAG-J kept NULL (3 rows: d124c95b — Aufhebung Entscheidung des
-- Amtes, 002c2ba7 — Folgemaßnahmen Validitätsentscheidung, 902cc5d5 —
-- Klärung Übersetzungsfragen). m will pick them up later via
-- /admin/rules. Existing rule_codes[] on these is left untouched.
--
-- Idempotent:
--   * Backfill UPDATEs guarded on `rule_code IS NULL` (the de-novo fill
--     bucket) — re-running is a no-op.
--   * Archive UPDATEs guarded on `is_active = true AND lifecycle_state
--     = 'published'` — re-running is a no-op.
--   * Normalization UPDATE guarded on `rule_code = 'RoP.49.1'` — no-op
--     after first apply.
--   * Prelim rename UPDATEs guarded on `name LIKE 'Vorab-Einrede%'` —
--     no-op after first apply or on prod (fermi already wrote).
--   * Snapshot table uses CREATE TABLE IF NOT EXISTS.
--   * Materialized-view refresh is safe to repeat.
--
-- audit_reason is set at the top via set_config(..., true) so the
-- mig-079 audit trigger on paliad.deadline_rules accepts the UPDATEs.

SELECT set_config(
    'paliad.audit_reason',
    'mig 097: t-paliad-210 legal-citation backfill — m''s FLAG walk-through 2026-05-18 (paliadin/head msg 2002). HIGH/MED proposals from docs/proposals/legal-citation-backfill-2026-05-18.md (commit 391be09) plus FLAG-A dedup + FLAG-B court-scheduled cites + FLAG-F rule_codes[] multi-cite + RoP.271.b on UPC initial submissions + RoP.49.1 padding normalization + R.19 prelim rename (fermi/t-paliad-207 consolidated)',
    true);

-- =============================================================================
-- 0. Backup snapshot of paliad.deadline_rules BEFORE the backfill. Full
--    table snapshot for the complete pre-097 baseline. Matches the
--    mig 096 pattern (proceeding_types_pre_096).
-- =============================================================================

CREATE TABLE IF NOT EXISTS paliad.deadline_rules_pre_097 AS
SELECT *, now() AS snapshotted_at
  FROM paliad.deadline_rules;

COMMENT ON TABLE paliad.deadline_rules_pre_097 IS
    'Snapshot of paliad.deadline_rules taken before mig 097 backfilled '
    'rule_code + legal_source + rule_codes[] across huygens''s HIGH/MED '
    'proposals (t-paliad-208) and m''s expanded FLAG walk-through '
    '(2026-05-18). Source-of-truth for the down migration; persists '
    'post-backfill as the permanent audit anchor — also retains the '
    'pre-dedup per-row rule_codes[] for the Mängelbeseitigung × 6 + '
    'Beginn-Hauptsache × 2 sets in case m later wants to recover the '
    'procedural-context citations.';

-- =============================================================================
-- 1. § 1 Easy wins (6 rows). legal_source already populated; only
--    rule_code missing. The 2 § 123 PatG Wiedereinsetzung twins
--    (c24d494c…, b588fa64…) are handled in § 4 below as part of the
--    FLAG-A dedup.
-- =============================================================================

UPDATE paliad.deadline_rules SET rule_code = '§ 253 ZPO'
 WHERE id = '1f532c82-9e6d-4f48-bd16-fa2fc71d5880' AND rule_code IS NULL;  -- de_inf.klage / Klageerhebung
UPDATE paliad.deadline_rules SET rule_code = '§ 339 ZPO'
 WHERE id = '20254f4e-d213-4cf6-8f5f-1d9d36eeb6ac' AND rule_code IS NULL;  -- Einspruch gegen Versäumnisurteil
UPDATE paliad.deadline_rules SET rule_code = '§ 296a ZPO'
 WHERE id = '3c36f149-3a81-456e-aac1-d4d18bfcb16b' AND rule_code IS NULL;  -- Schriftsatznachreichung
UPDATE paliad.deadline_rules SET rule_code = 'R. 135 EPÜ'
 WHERE id = 'f1099cf6-4c87-430e-b1c5-488bd44cb143' AND rule_code IS NULL;  -- Weiterbehandlungsantrag (Art. 121 EPÜ)
UPDATE paliad.deadline_rules SET rule_code = '§ 234 ZPO'
 WHERE id = 'd40d9be7-e1b6-451c-bee2-6eaee2307ec5' AND rule_code IS NULL;  -- Wiedereinsetzungsantrag (§ 233 ZPO)
UPDATE paliad.deadline_rules SET rule_code = 'R. 136 EPÜ'
 WHERE id = '23c6f445-4ed2-4ade-8ea0-c4ab6b364bb6' AND rule_code IS NULL;  -- Wiedereinsetzungsantrag (Art. 122 EPÜ)

-- =============================================================================
-- 2. § 2 Proceeding-typed HIGH/MED (15 rows). rule_code + legal_source.
--    Note: rule_codes[] is set in § 9 for the 5 UPC initial-submission
--    rows (inf.soc / rev.app / pi.app / damages.app / disc.app) to
--    include the RoP.271.b secondary cite. For DE/EPA rows here,
--    rule_codes[] is left untouched (currently NULL and not used for
--    DE/EPA citations in this corpus).
-- =============================================================================

UPDATE paliad.deadline_rules SET rule_code = 'RoP.013.1',  legal_source = 'UPC.RoP.13.1'
 WHERE id = '42be6c9b-8e84-4804-962f-94c3315aca1b' AND rule_code IS NULL;  -- upc.inf.cfi / inf.soc
UPDATE paliad.deadline_rules SET rule_code = 'RoP.042',    legal_source = 'UPC.RoP.42'
 WHERE id = '995c108e-e73a-4f9c-b79f-47abe7c94108' AND rule_code IS NULL;  -- upc.rev.cfi / rev.app
UPDATE paliad.deadline_rules SET rule_code = 'RoP.206',    legal_source = 'UPC.RoP.206'
 WHERE id = 'ed0194b7-74ab-4402-8971-7211f6036ff9' AND rule_code IS NULL;  -- upc.pi.cfi / pi.app
UPDATE paliad.deadline_rules SET rule_code = 'RoP.243',    legal_source = 'UPC.RoP.243',    rule_codes = ARRAY['RoP.243']::text[]
 WHERE id = '85f92b72-c654-4429-8e91-03402f9438c6' AND rule_code IS NULL;  -- upc.apl.merits / app.oral
UPDATE paliad.deadline_rules SET rule_code = 'RoP.131',    legal_source = 'UPC.RoP.131'
 WHERE id = '3e1719e8-f6f6-4260-8f02-754bd214937f' AND rule_code IS NULL;  -- upc.dmgs.cfi / damages.app
UPDATE paliad.deadline_rules SET rule_code = 'RoP.141',    legal_source = 'UPC.RoP.141'
 WHERE id = 'eb1fa1d1-b345-42ba-ab14-79f5284166b0' AND rule_code IS NULL;  -- upc.disc.cfi / disc.app
UPDATE paliad.deadline_rules SET rule_code = '§ 81 PatG',  legal_source = 'DE.PatG.81.1'
 WHERE id = 'ba33e704-18f6-4486-8107-abdb1e9cbfad' AND rule_code IS NULL;  -- de.null.bpatg / de_null.klage
UPDATE paliad.deadline_rules SET rule_code = '§ 58 PatG',  legal_source = 'DE.PatG.58.1'
 WHERE id = '972f8fe4-8f4c-4497-9736-d60399ae5989' AND rule_code IS NULL;  -- dpma.opp.dpma / dpma_opp.publish
UPDATE paliad.deadline_rules SET rule_code = 'Art. 75 EPÜ',  legal_source = 'EU.EPÜ.75'
 WHERE id = 'a1766364-1478-4b13-ae02-0a94367c585e' AND rule_code IS NULL;  -- epa.grant.exa / ep_grant.filing
UPDATE paliad.deadline_rules SET rule_code = 'Art. 92 EPÜ',  legal_source = 'EU.EPÜ.92'
 WHERE id = '63069ae5-e380-4db5-b020-d1856f31300c' AND rule_code IS NULL;  -- epa.grant.exa / ep_grant.search
UPDATE paliad.deadline_rules SET rule_code = 'Art. 97 EPÜ',  legal_source = 'EU.EPÜ.97.1'
 WHERE id = '86b3a295-d76b-4566-955d-55f7a394524e' AND rule_code IS NULL;  -- epa.grant.exa / ep_grant.grant
UPDATE paliad.deadline_rules SET rule_code = 'Art. 97 EPÜ',  legal_source = 'EU.EPÜ.97.3'
 WHERE id = '520dd205-7b4a-45f4-b87f-e2be5d1e183e' AND rule_code IS NULL;  -- epa.opp.opd / epa_opp.grant
UPDATE paliad.deadline_rules SET rule_code = 'Art. 101 EPÜ', legal_source = 'EU.EPÜ.101'
 WHERE id = '8961a54b-2645-4af4-b0f5-114128150839' AND rule_code IS NULL;  -- epa.opp.opd / epa_opp.entsch
UPDATE paliad.deadline_rules SET rule_code = 'Art. 116 EPÜ', legal_source = 'EU.EPÜ.116'
 WHERE id = '926f333d-55d2-4a12-890e-0508a4ea1bd4' AND rule_code IS NULL;  -- epa.opp.boa / epa_app.oral
UPDATE paliad.deadline_rules SET rule_code = 'Art. 111 EPÜ', legal_source = 'EU.EPÜ.111'
 WHERE id = 'd0949eaf-da69-4972-90c2-7e6c1bebcd79' AND rule_code IS NULL;  -- epa.opp.boa / epa_app.entsch2

-- =============================================================================
-- 3. § 3 Orphan HIGH/MED (47 rows). rule_code + legal_source. For UPC
--    rows also normalize rule_codes[] to ARRAY[rule_code] so the
--    structured tooling field matches the display field. The orphan
--    archive destinations (5c0508f4 / 791fd0f7 / d886f46f) are NOT
--    filled here — they're flipped to archived in § 4.
-- =============================================================================

-- § 3.1 main-pleadings track (10 rows)
UPDATE paliad.deadline_rules SET rule_code = 'RoP.023',     legal_source = 'UPC.RoP.23.1',     rule_codes = ARRAY['RoP.023']::text[]
 WHERE id = 'e34097d6-670d-447a-bdfe-b42df20ba459' AND rule_code IS NULL;  -- Klageerwiderung
UPDATE paliad.deadline_rules SET rule_code = 'RoP.025.1',   legal_source = 'UPC.RoP.25.1',     rule_codes = ARRAY['RoP.025.1']::text[]
 WHERE id = '7d8a4804-0ebc-42c4-8552-624350cd81f3' AND rule_code IS NULL;  -- Nichtigkeitswiderklage
UPDATE paliad.deadline_rules SET rule_code = 'RoP.049.2.b', legal_source = 'UPC.RoP.49.2.b',   rule_codes = ARRAY['RoP.049.2.b']::text[]
 WHERE id = 'c7523e6b-579d-4d80-afb3-e1cf11238d40' AND rule_code IS NULL;  -- Verletzungswiderklage
UPDATE paliad.deadline_rules SET rule_code = 'RoP.019.1',   legal_source = 'UPC.RoP.19.1',     rule_codes = ARRAY['RoP.019.1']::text[]
 WHERE id = 'c57f62f8-bb52-4232-be85-9125fa93f58c' AND rule_code IS NULL;  -- Vorgängige Einrede
UPDATE paliad.deadline_rules SET rule_code = 'RoP.029.b',   legal_source = 'UPC.RoP.29.b',     rule_codes = ARRAY['RoP.029.b']::text[]
 WHERE id = '84b390e0-1ca4-461a-942c-4ad94c643750' AND rule_code IS NULL;  -- Replik auf Klageerwiderung
UPDATE paliad.deadline_rules SET rule_code = 'RoP.029.c',   legal_source = 'UPC.RoP.29.c',     rule_codes = ARRAY['RoP.029.c']::text[]
 WHERE id = '176cc1ca-2b25-49ee-9c3e-8afed1673b7d' AND rule_code IS NULL;  -- Duplik Replik Klageerwiderung
UPDATE paliad.deadline_rules SET rule_code = 'RoP.049.1',   legal_source = 'UPC.RoP.49.1',     rule_codes = ARRAY['RoP.049.1']::text[]
 WHERE id = 'a32dcec1-6aaa-4a3c-936c-9a761d9362f0' AND rule_code IS NULL;  -- Erwiderung auf Nichtigkeitsklage
UPDATE paliad.deadline_rules SET rule_code = 'RoP.052',     legal_source = 'UPC.RoP.52',       rule_codes = ARRAY['RoP.052']::text[]
 WHERE id = '1b5c6dee-0032-4be8-864c-f2ab945aacc5' AND rule_code IS NULL;  -- Duplik Replik Erwiderung Nichtigkeitsklage
UPDATE paliad.deadline_rules SET rule_code = 'RoP.056.1',   legal_source = 'UPC.RoP.56.1',     rule_codes = ARRAY['RoP.056.1']::text[]
 WHERE id = 'bea86f9b-37d5-4f6e-b6bd-f0c01f053b66' AND rule_code IS NULL;  -- Erwiderung auf Verletzungswiderklage
UPDATE paliad.deadline_rules SET rule_code = 'RoP.056.3',   legal_source = 'UPC.RoP.56.3',     rule_codes = ARRAY['RoP.056.3']::text[]
 WHERE id = '4834c957-2518-40e9-ad62-447f3f220d33' AND rule_code IS NULL;  -- Replik Erwiderung Verletzungswiderklage

-- § 3.2 Patentänderungs-Track (1 row; FLAG-G twin rows are handled in § 9)
UPDATE paliad.deadline_rules SET rule_code = 'RoP.032.1', legal_source = 'UPC.RoP.32.1', rule_codes = ARRAY['RoP.032.1']::text[]
 WHERE id = '7e65a434-f5c6-4391-a65c-d02de735f551' AND rule_code IS NULL;  -- Erwiderung auf Patentänderungsantrag
UPDATE paliad.deadline_rules SET rule_code = 'RoP.032.3', legal_source = 'UPC.RoP.32.3', rule_codes = ARRAY['RoP.032.3']::text[]
 WHERE id = 'dfd52792-840f-42c4-8b71-0f77d07cbb53' AND rule_code IS NULL;  -- Replik Erwiderung Patentänderung
UPDATE paliad.deadline_rules SET rule_code = 'RoP.032.3', legal_source = 'UPC.RoP.32.3', rule_codes = ARRAY['RoP.032.3']::text[]
 WHERE id = '8cdf54eb-5189-47fd-a390-6a0ee98e5243' AND rule_code IS NULL;  -- Duplik Replik Erwiderung Patentänderung

-- § 3.3 appeal track (8 fills; 2 archive-destinations handled in § 4)
UPDATE paliad.deadline_rules SET rule_code = 'RoP.224.1.a', legal_source = 'UPC.RoP.224.1.a', rule_codes = ARRAY['RoP.224.1.a']::text[]
 WHERE id = '1dfba5b1-4ed1-40c1-9cf6-4ed8ff7a0818' AND rule_code IS NULL;  -- Berufungsschrift canonical
UPDATE paliad.deadline_rules SET rule_code = 'RoP.224.1.b', legal_source = 'UPC.RoP.224.1.b', rule_codes = ARRAY['RoP.224.1.b']::text[]
 WHERE id = 'd560b3b6-9437-4b22-b62c-957d4a37d21a' AND rule_code IS NULL;  -- Berufungsschrift Orders
UPDATE paliad.deadline_rules SET rule_code = 'RoP.225.1',   legal_source = 'UPC.RoP.225.1',   rule_codes = ARRAY['RoP.225.1']::text[]
 WHERE id = '573df3d1-8ea2-4a6e-b0d4-fc3cd10506da' AND rule_code IS NULL;  -- Berufungsbegründung canonical
UPDATE paliad.deadline_rules SET rule_code = 'RoP.224.1.b', legal_source = 'UPC.RoP.224.1.b', rule_codes = ARRAY['RoP.224.1.b']::text[]
 WHERE id = '91e367dd-ffe6-4012-ac6a-b61c32e2b3b7' AND rule_code IS NULL;  -- Berufung (Anordnungen & mit Zulassung)
UPDATE paliad.deadline_rules SET rule_code = 'RoP.221.1',   legal_source = 'UPC.RoP.221.1',   rule_codes = ARRAY['RoP.221.1']::text[]
 WHERE id = 'ccb916df-4ee3-4dde-bcb0-6a5b557c0cba' AND rule_code IS NULL;  -- Berufungszulassung Kosten
UPDATE paliad.deadline_rules SET rule_code = 'RoP.220.3',   legal_source = 'UPC.RoP.220.3',   rule_codes = ARRAY['RoP.220.3']::text[]
 WHERE id = '342e749d-c2bc-4148-974b-ac0331b76229' AND rule_code IS NULL;  -- Ermessensüberprüfung
UPDATE paliad.deadline_rules SET rule_code = 'RoP.235.1',   legal_source = 'UPC.RoP.235.1',   rule_codes = ARRAY['RoP.235.1']::text[]
 WHERE id = '10374392-b8db-4738-8a61-f8ce0fabcc3e' AND rule_code IS NULL;  -- Berufungserwiderung (224.2(a))
UPDATE paliad.deadline_rules SET rule_code = 'RoP.237.1',   legal_source = 'UPC.RoP.237.1',   rule_codes = ARRAY['RoP.237.1']::text[]
 WHERE id = '6e39b653-1328-40e1-95f1-071fdf46eed6' AND rule_code IS NULL;  -- Anschlussberufung (224.2(a))
UPDATE paliad.deadline_rules SET rule_code = 'RoP.238.1',   legal_source = 'UPC.RoP.238.1',   rule_codes = ARRAY['RoP.238.1']::text[]
 WHERE id = '6b989e85-e739-4e3b-bfd1-52b0e0c35f61' AND rule_code IS NULL;  -- Erwiderung Anschlussberufung (224.2(a))
UPDATE paliad.deadline_rules SET rule_code = 'RoP.238.2',   legal_source = 'UPC.RoP.238.2',   rule_codes = ARRAY['RoP.238.2']::text[]
 WHERE id = 'e78f4652-acf9-4ecd-ac48-888ce475173f' AND rule_code IS NULL;  -- Erwiderung Anschlussberufung (224.2(b))

-- § 3.4 Schadensbemessung / Rechnungslegung (7 rows)
UPDATE paliad.deadline_rules SET rule_code = 'RoP.137.2', legal_source = 'UPC.RoP.137.2', rule_codes = ARRAY['RoP.137.2']::text[]
 WHERE id = 'd414f603-14c1-49f2-91be-e305eba696e3' AND rule_code IS NULL;  -- Erwiderung Schadensbemessung
UPDATE paliad.deadline_rules SET rule_code = 'RoP.139',   legal_source = 'UPC.RoP.139',   rule_codes = ARRAY['RoP.139']::text[]
 WHERE id = '9f39e263-e9ec-4805-a82e-c7551a22c78d' AND rule_code IS NULL;  -- Replik Schadensbemessung
UPDATE paliad.deadline_rules SET rule_code = 'RoP.139',   legal_source = 'UPC.RoP.139',   rule_codes = ARRAY['RoP.139']::text[]
 WHERE id = '067ffdf0-180b-488f-a369-249f6bcb9faa' AND rule_code IS NULL;  -- Duplik Schadensbemessung
UPDATE paliad.deadline_rules SET rule_code = 'RoP.142.2', legal_source = 'UPC.RoP.142.2', rule_codes = ARRAY['RoP.142.2']::text[]
 WHERE id = '429b8ec0-227a-4945-8b20-6ad79330a490' AND rule_code IS NULL;  -- Erwiderung Rechnungslegung
UPDATE paliad.deadline_rules SET rule_code = 'RoP.142.3', legal_source = 'UPC.RoP.142.3', rule_codes = ARRAY['RoP.142.3']::text[]
 WHERE id = '8d36fc76-61b9-4e99-b113-eed4c9c4b2c7' AND rule_code IS NULL;  -- Replik Rechnungslegung
UPDATE paliad.deadline_rules SET rule_code = 'RoP.142.3', legal_source = 'UPC.RoP.142.3', rule_codes = ARRAY['RoP.142.3']::text[]
 WHERE id = 'ed82fec9-2346-494f-a0ff-f41e64c26942' AND rule_code IS NULL;  -- Duplik Rechnungslegung
UPDATE paliad.deadline_rules SET rule_code = 'RoP.151',   legal_source = 'UPC.RoP.151',   rule_codes = ARRAY['RoP.151']::text[]
 WHERE id = 'eed69e8b-0dc8-4d97-83f0-5694d539b46a' AND rule_code IS NULL;  -- Kostenentscheidung

-- § 3.5 provisional / PI (2 rows; canonical ba335c99 + the d886f46f archive handled in § 4)
UPDATE paliad.deadline_rules SET rule_code = 'RoP.197.3', legal_source = 'UPC.RoP.197.3', rule_codes = ARRAY['RoP.197.3']::text[]
 WHERE id = '1f1f72ef-5a67-4d6a-9a80-82e53375177a' AND rule_code IS NULL;  -- Beweissicherungsanordnung
UPDATE paliad.deadline_rules SET rule_code = 'RoP.207.9', legal_source = 'UPC.RoP.207.9', rule_codes = ARRAY['RoP.207.9']::text[]
 WHERE id = '3e2f5697-3012-4bae-bd4d-44998dd3b75b' AND rule_code IS NULL;  -- Schutzschrift

-- § 3.7 formalities / Registry (4 fills; 5 Mängelbeseitigung dups + FLAG-J 2 rows handled separately)
UPDATE paliad.deadline_rules SET rule_code = 'RoP.016.5', legal_source = 'UPC.RoP.16.5', rule_codes = ARRAY['RoP.016.5']::text[]
 WHERE id = '3bc40027-9ebf-4f3d-880d-bf9de6da3ec0' AND rule_code IS NULL;  -- Mängelbeseitigung / Stellungnahme
UPDATE paliad.deadline_rules SET rule_code = 'RoP.262.2', legal_source = 'UPC.RoP.262.2', rule_codes = ARRAY['RoP.262.2']::text[]
 WHERE id = '69e356b7-79b3-42d7-972b-44d4e35ebdbc' AND rule_code IS NULL;  -- Vertraulichkeit
UPDATE paliad.deadline_rules SET rule_code = 'RoP.353',   legal_source = 'UPC.RoP.353',   rule_codes = ARRAY['RoP.353']::text[]
 WHERE id = '57e6eeca-8695-4af3-96cc-16ebd8bc3f2c' AND rule_code IS NULL;  -- Berichtigung
UPDATE paliad.deadline_rules SET rule_code = 'RoP.333.1', legal_source = 'UPC.RoP.333.1', rule_codes = ARRAY['RoP.333.1']::text[]
 WHERE id = '8ec233b9-3bc4-4015-a158-86af233e52b3' AND rule_code IS NULL;  -- Verfahrensleitende Anordnung

-- § 3.8 translation / interpretation (1 row; FLAG-H/J handled in § 9 / left NULL)
UPDATE paliad.deadline_rules SET rule_code = 'RoP.109.1', legal_source = 'UPC.RoP.109.1', rule_codes = ARRAY['RoP.109.1']::text[]
 WHERE id = 'bb7bafcb-9d91-4bf7-ae2c-6634652d9906' AND rule_code IS NULL;  -- Simultanübersetzung

-- § 3.9 review / rehearing (2 rows)
UPDATE paliad.deadline_rules SET rule_code = 'RoP.247.2', legal_source = 'UPC.RoP.247.2', rule_codes = ARRAY['RoP.247.2']::text[]
 WHERE id = '372e86e3-c8ff-4cb5-9389-66acdbc96e57' AND rule_code IS NULL;  -- Wiederaufnahme (schwerwiegend)
UPDATE paliad.deadline_rules SET rule_code = 'RoP.247.2', legal_source = 'UPC.RoP.247.2', rule_codes = ARRAY['RoP.247.2']::text[]
 WHERE id = '58de9573-07db-4d8d-9b00-8fab0d71d88c' AND rule_code IS NULL;  -- Wiederaufnahme (Straftat)

-- =============================================================================
-- 4. § 4 FLAG-A dedup (clean only). 1 canonical fill (the other 2
--    canonicals are filled in § 3.3) + 3 archive flips. Canonical
--    selection per m's spec: lowest UUID. None of the archive
--    candidates have FK references in mgmt.deadline_rules / paliad.
--    appointments / paliad.deadlines / paliad.deadline_rules (parent_id
--    or draft_of) — verified pre-mig. Archive over DELETE per m
--    (audit trail).
--
--    Mängelbeseitigung 6× and Beginn-Hauptsache 2× are intentionally
--    NOT deduped in this mig — see header for the deferred-decision
--    rationale. Their rows stay active+published+rule_code IS NULL
--    until m's call lands.
-- =============================================================================

-- Canonical fill for the § 123 PatG twin (legal_source already
-- DE.PatG.123.2). The other 2 canonicals (Berufungsschrift 1dfba5b1
-- and Berufungsbegründung 573df3d1) are filled in § 3.3 above.
UPDATE paliad.deadline_rules SET rule_code = '§ 123 PatG'
 WHERE id = 'b588fa64-a727-4cfb-a45d-69a835a3b05a' AND rule_code IS NULL;

-- Archive flips (3 rows: the non-canonical sides of the 3 clean dedup
-- sets). After this each set has exactly 1 active+published row.
UPDATE paliad.deadline_rules
   SET is_active = false, lifecycle_state = 'archived'
 WHERE id IN (
        'c24d494c-0da1-4f01-aa74-0f37f99fe1ae',  -- Wiedereinsetzung § 123 PatG dup
        '5c0508f4-020a-4ef5-bcc7-1ee85eafe0b3',  -- Berufungsschrift dup
        '791fd0f7-a448-4711-b1aa-63e6df1e7c57'   -- Berufungsbegründung dup
   )
   AND is_active = true
   AND lifecycle_state = 'published';

-- =============================================================================
-- 5. § 5 FLAG-B court-scheduled events (26 rows). Cite the framing norm
--    that authorises the court to schedule the event. UPC RoP.111 /
--    RoP.118 / RoP.101 / RoP.209 / RoP.211 / RoP.350 / RoP.220.1.c /
--    RoP.157. DE § 285 ZPO / § 300 ZPO / § 89 PatG / § 84 PatG / § 113
--    PatG / § 119 PatG. DPMA § 47 / 78 / 79 / 107 PatG.
-- =============================================================================

-- UPC court-scheduled events
UPDATE paliad.deadline_rules SET rule_code = 'RoP.118', legal_source = 'UPC.RoP.118', rule_codes = ARRAY['RoP.118']::text[]
 WHERE id = '60d71f1e-a0e8-42cd-85e9-89f3c808868f' AND rule_code IS NULL;  -- inf.decision
UPDATE paliad.deadline_rules SET rule_code = 'RoP.101', legal_source = 'UPC.RoP.101', rule_codes = ARRAY['RoP.101']::text[]
 WHERE id = '7b118633-92b2-4c91-8512-6cb929288f10' AND rule_code IS NULL;  -- inf.interim
UPDATE paliad.deadline_rules SET rule_code = 'RoP.111', legal_source = 'UPC.RoP.111', rule_codes = ARRAY['RoP.111']::text[]
 WHERE id = 'd4c01a6f-d147-4505-bf1c-9aaf88b15287' AND rule_code IS NULL;  -- inf.oral
UPDATE paliad.deadline_rules SET rule_code = 'RoP.118', legal_source = 'UPC.RoP.118', rule_codes = ARRAY['RoP.118']::text[]
 WHERE id = 'f382cfe4-6703-40f8-a43d-0fe02d62d0fa' AND rule_code IS NULL;  -- rev.decision
UPDATE paliad.deadline_rules SET rule_code = 'RoP.101', legal_source = 'UPC.RoP.101', rule_codes = ARRAY['RoP.101']::text[]
 WHERE id = 'ccad91ef-da04-4b81-a979-658578fb97c4' AND rule_code IS NULL;  -- rev.interim
UPDATE paliad.deadline_rules SET rule_code = 'RoP.111', legal_source = 'UPC.RoP.111', rule_codes = ARRAY['RoP.111']::text[]
 WHERE id = '38e8982b-5cc9-41b3-b477-37ce4bd4e7c4' AND rule_code IS NULL;  -- rev.oral
UPDATE paliad.deadline_rules SET rule_code = 'RoP.209',   legal_source = 'UPC.RoP.209',   rule_codes = ARRAY['RoP.209']::text[]
 WHERE id = 'e4a61ebf-c49b-450f-9d94-bb06098536b4' AND rule_code IS NULL;  -- pi.oral
UPDATE paliad.deadline_rules SET rule_code = 'RoP.211',   legal_source = 'UPC.RoP.211',   rule_codes = ARRAY['RoP.211']::text[]
 WHERE id = '7b93a8b7-115d-42b4-9d1d-34684ddf5206' AND rule_code IS NULL;  -- pi.order
UPDATE paliad.deadline_rules SET rule_code = 'RoP.209.1', legal_source = 'UPC.RoP.209.1', rule_codes = ARRAY['RoP.209.1']::text[]
 WHERE id = '30ffe572-aa77-4dcb-9292-a4750289f75c' AND rule_code IS NULL;  -- pi.response (court-set)
UPDATE paliad.deadline_rules SET rule_code = 'RoP.350',     legal_source = 'UPC.RoP.350',     rule_codes = ARRAY['RoP.350']::text[]
 WHERE id = '685bad4f-3c3e-425d-8839-2f765d0fc96e' AND rule_code IS NULL;  -- app.decision
UPDATE paliad.deadline_rules SET rule_code = 'RoP.220.1.c', legal_source = 'UPC.RoP.220.1.c', rule_codes = ARRAY['RoP.220.1.c']::text[]
 WHERE id = 'c2865575-d7d6-436d-b61c-0a266217f76c' AND rule_code IS NULL;  -- app_ord.order
UPDATE paliad.deadline_rules SET rule_code = 'RoP.157',     legal_source = 'UPC.RoP.157',     rule_codes = ARRAY['RoP.157']::text[]
 WHERE id = '01db67c9-5621-48ca-9dbd-d652b6237b24' AND rule_code IS NULL;  -- cost.decision

-- DE court-scheduled events
UPDATE paliad.deadline_rules SET rule_code = '§ 285 ZPO', legal_source = 'DE.ZPO.285'
 WHERE id = 'a95af317-2fdb-43c9-ab66-c8b2099aaa5a' AND rule_code IS NULL;  -- de_inf.termin
UPDATE paliad.deadline_rules SET rule_code = '§ 300 ZPO', legal_source = 'DE.ZPO.300'
 WHERE id = 'e46d2ae7-74bf-4c06-9e55-921242d36f2a' AND rule_code IS NULL;  -- de_inf.urteil
UPDATE paliad.deadline_rules SET rule_code = '§ 285 ZPO', legal_source = 'DE.ZPO.285'
 WHERE id = '2a16f77f-408f-48c4-9d71-8ea5926d4dca' AND rule_code IS NULL;  -- de_inf_olg.termin
UPDATE paliad.deadline_rules SET rule_code = '§ 300 ZPO', legal_source = 'DE.ZPO.300'
 WHERE id = '7d7d88c5-895e-4855-8f4d-2e160ff74998' AND rule_code IS NULL;  -- de_inf_olg.urteil_olg
UPDATE paliad.deadline_rules SET rule_code = '§ 285 ZPO', legal_source = 'DE.ZPO.285'
 WHERE id = 'b1460f90-419e-47ae-978a-8e32ffafad73' AND rule_code IS NULL;  -- de_inf_bgh.termin
UPDATE paliad.deadline_rules SET rule_code = '§ 300 ZPO', legal_source = 'DE.ZPO.300'
 WHERE id = '803460ac-f6bd-4194-b5ab-140175644648' AND rule_code IS NULL;  -- de_inf_bgh.urteil_bgh
UPDATE paliad.deadline_rules SET rule_code = '§ 89 PatG', legal_source = 'DE.PatG.89'
 WHERE id = 'ab60e712-bc56-4326-8df0-413881996bf3' AND rule_code IS NULL;  -- de_null.termin
UPDATE paliad.deadline_rules SET rule_code = '§ 84 PatG', legal_source = 'DE.PatG.84'
 WHERE id = '1476829a-cc92-4221-b182-846fc99ad941' AND rule_code IS NULL;  -- de_null.urteil
UPDATE paliad.deadline_rules SET rule_code = '§ 113 PatG', legal_source = 'DE.PatG.113'
 WHERE id = 'd077816d-bce4-4cb7-bd67-7b52edbf7fb9' AND rule_code IS NULL;  -- de_null_bgh.termin
UPDATE paliad.deadline_rules SET rule_code = '§ 119 PatG', legal_source = 'DE.PatG.119'
 WHERE id = '816e9756-efff-4e40-b650-f0b31bdc21e5' AND rule_code IS NULL;  -- de_null_bgh.urteil_bgh

-- DPMA / BPatG / BGH-PatG court-scheduled events
UPDATE paliad.deadline_rules SET rule_code = '§ 47 PatG', legal_source = 'DE.PatG.47'
 WHERE id = '193a85e2-5794-463a-8c45-73174a54cea9' AND rule_code IS NULL;  -- dpma_opp.entscheidung
UPDATE paliad.deadline_rules SET rule_code = '§ 79 PatG', legal_source = 'DE.PatG.79'
 WHERE id = 'baaff831-6a3f-43ed-96bb-eae6ad73f6fc' AND rule_code IS NULL;  -- dpma_bpatg.entsch_bpatg
UPDATE paliad.deadline_rules SET rule_code = '§ 78 PatG', legal_source = 'DE.PatG.78'
 WHERE id = '446694c2-5b34-4ecd-9bf7-7eee055b0d1b' AND rule_code IS NULL;  -- dpma_bpatg.termin
UPDATE paliad.deadline_rules SET rule_code = '§ 107 PatG', legal_source = 'DE.PatG.107'
 WHERE id = '99c02992-1a77-4694-b773-941ac9876bb5' AND rule_code IS NULL;  -- dpma_bgh.entsch_bgh

-- =============================================================================
-- 6. § 6 FLAG-C/D rubber-stamp (5 rows). UPC RoP duration-vs-norm
--    mismatches get the canonical citation per m ("just go ahead"). DE
--    LG patent-practice 4-week replik/duplik cite § 273 ZPO (court-set
--    framing).
-- =============================================================================

UPDATE paliad.deadline_rules SET rule_code = 'RoP.052',   legal_source = 'UPC.RoP.52',    rule_codes = ARRAY['RoP.052']::text[]
 WHERE id = '7e0ea937-d81b-4dee-897e-0d8bc0543f34' AND rule_code IS NULL;  -- rev.reply (FLAG-C)
UPDATE paliad.deadline_rules SET rule_code = 'RoP.052',   legal_source = 'UPC.RoP.52',    rule_codes = ARRAY['RoP.052']::text[]
 WHERE id = 'b7890351-c6d6-46e4-b064-0513a1808e6d' AND rule_code IS NULL;  -- rev.rejoin (FLAG-C)
UPDATE paliad.deadline_rules SET rule_code = 'RoP.235.1', legal_source = 'UPC.RoP.235.1', rule_codes = ARRAY['RoP.235.1']::text[]
 WHERE id = 'd6600ceb-d1d5-408a-a7c9-1026f304ac7f' AND rule_code IS NULL;  -- app.response (FLAG-C)
UPDATE paliad.deadline_rules SET rule_code = '§ 273 ZPO', legal_source = 'DE.ZPO.273'
 WHERE id = 'd46d915e-fd46-4167-88b5-6d22bcbb8882' AND rule_code IS NULL;  -- de_inf.replik (FLAG-D)
UPDATE paliad.deadline_rules SET rule_code = '§ 273 ZPO', legal_source = 'DE.ZPO.273'
 WHERE id = 'ca9b52cb-e986-4c3a-9e89-e799e6a6ac33' AND rule_code IS NULL;  -- de_inf.duplik (FLAG-D)

-- =============================================================================
-- 7. § 7 FLAG-E service triggers (6 rows, DE/EPA). § 317 ZPO for LG/OLG
--    judgment-service, § 99 / § 47 / § 79 PatG for the PatG variants,
--    R. 111 EPÜ for EPA notification.
-- =============================================================================

UPDATE paliad.deadline_rules SET rule_code = '§ 317 ZPO', legal_source = 'DE.ZPO.317'
 WHERE id = '106d8a0b-514b-4021-8b65-7debff71f1d3' AND rule_code IS NULL;  -- de_inf_olg.urteil_lg
UPDATE paliad.deadline_rules SET rule_code = '§ 317 ZPO', legal_source = 'DE.ZPO.317'
 WHERE id = 'd071b5c6-f33e-44e8-8656-4e9cccf55701' AND rule_code IS NULL;  -- de_inf_bgh.urteil_olg
UPDATE paliad.deadline_rules SET rule_code = '§ 99 PatG', legal_source = 'DE.PatG.99.1'
 WHERE id = 'bdae7319-7435-40e9-be19-6ce21fdb9946' AND rule_code IS NULL;  -- de_null_bgh.urteil_bpatg
UPDATE paliad.deadline_rules SET rule_code = '§ 47 PatG', legal_source = 'DE.PatG.47.1'
 WHERE id = '327390f9-3c1b-496f-8e63-2bf19c380dfe' AND rule_code IS NULL;  -- dpma_bpatg.entscheidung
UPDATE paliad.deadline_rules SET rule_code = '§ 79 PatG', legal_source = 'DE.PatG.79.1'
 WHERE id = 'd3ea5e50-f7e2-40f1-bb16-30664acc2e2b' AND rule_code IS NULL;  -- dpma_bgh.entsch_bpatg
UPDATE paliad.deadline_rules SET rule_code = 'R. 111 EPÜ', legal_source = 'EU.EPC-R.111'
 WHERE id = '79c27f9b-5195-4272-90d6-ea6a43cd0938' AND rule_code IS NULL;  -- epa_app.entsch

-- =============================================================================
-- 8. § 8 FLAG-F combined-pleading rows (5 rows). Primary cite in
--    rule_code + legal_source; full set of citations in rule_codes[]
--    so downstream tooling can resolve any of the combined norms.
-- =============================================================================

UPDATE paliad.deadline_rules
   SET rule_code = 'RoP.029.a', legal_source = 'UPC.RoP.29.a',
       rule_codes = ARRAY['RoP.029.a', 'RoP.029.b']::text[]
 WHERE id = 'cec1a865-30a4-46c9-8abf-630d4478b91a' AND rule_code IS NULL;  -- Erwid CCR + Replik SoD

UPDATE paliad.deadline_rules
   SET rule_code = 'RoP.029.c', legal_source = 'UPC.RoP.29.c',
       rule_codes = ARRAY['RoP.029.c', 'RoP.032.3']::text[]
 WHERE id = '02ae9c1f-2aa0-4e0e-acf1-ae235588a64f' AND rule_code IS NULL;  -- Duplik Replik + Replik Erwid Patentänderung

UPDATE paliad.deadline_rules
   SET rule_code = 'RoP.029.d', legal_source = 'UPC.RoP.29.d',
       rule_codes = ARRAY['RoP.029.d', 'RoP.029.c', 'RoP.032.1']::text[]
 WHERE id = 'ec2a1274-ffd8-42e7-9e27-582365d04d6e' AND rule_code IS NULL;  -- Replik Erwid Widerklage + Duplik Replik Klageerwid + Erwid Patentänderung

UPDATE paliad.deadline_rules
   SET rule_code = 'RoP.051', legal_source = 'UPC.RoP.51',
       rule_codes = ARRAY['RoP.051', 'RoP.049.2.a', 'RoP.056.1']::text[]
 WHERE id = '37bd034b-79e3-4c3c-a21d-b078aaf2ea04' AND rule_code IS NULL;  -- Replik Erwid Nichtigkeit + Erwid Patent + Erwid Widerklage

UPDATE paliad.deadline_rules
   SET rule_code = 'RoP.056.4', legal_source = 'UPC.RoP.56.4',
       rule_codes = ARRAY['RoP.056.4', 'RoP.032.3']::text[]
 WHERE id = '7b548c48-6fef-4387-8123-e1f1e4ee6da2' AND rule_code IS NULL;  -- Duplik (Verletzungswiderklage + Patentänderung)

-- =============================================================================
-- 9. § 9 FLAG-G/H/I + RoP.271.b. Patentänderung INF/REV split (G),
--    sub-paragraph spot-checks (H, applied as-is per doc), negative-
--    declaration RoP.069 by analogy (I), and the RoP.271.b 10-day
--    service-deferral secondary cite on UPC initial submissions.
-- =============================================================================

-- FLAG-G: Patentänderungs-Twin (INF vs REV context)
UPDATE paliad.deadline_rules SET rule_code = 'RoP.030.1',   legal_source = 'UPC.RoP.30.1',   rule_codes = ARRAY['RoP.030.1']::text[]
 WHERE id = 'fb7050c6-a18b-47e4-8811-46ca3677d549' AND rule_code IS NULL;  -- Patentänderung INF
UPDATE paliad.deadline_rules SET rule_code = 'RoP.049.2.a', legal_source = 'UPC.RoP.49.2.a', rule_codes = ARRAY['RoP.049.2.a']::text[]
 WHERE id = '21e67ac1-fe40-44d1-ae2e-ea90e0b97598' AND rule_code IS NULL;  -- Patentänderung REV

-- FLAG-H: sub-paragraph spot-checks (8 rows, applied per doc proposal)
UPDATE paliad.deadline_rules SET rule_code = 'RoP.225.2', legal_source = 'UPC.RoP.225.2', rule_codes = ARRAY['RoP.225.2']::text[]
 WHERE id = 'c3a369f9-4f56-4c88-b11c-f98d05d3b376' AND rule_code IS NULL;  -- Berufungsbegründung Orders
UPDATE paliad.deadline_rules SET rule_code = 'RoP.234.1', legal_source = 'UPC.RoP.234.1', rule_codes = ARRAY['RoP.234.1']::text[]
 WHERE id = 'd4f739cd-444d-48c0-98c4-70f0521b4916' AND rule_code IS NULL;  -- Anfechtung Verwerfung
UPDATE paliad.deadline_rules SET rule_code = 'RoP.235.4', legal_source = 'UPC.RoP.235.4', rule_codes = ARRAY['RoP.235.4']::text[]
 WHERE id = '4c585c6d-fb5c-4a99-a798-86a05c757bf7' AND rule_code IS NULL;  -- Berufungserwiderung Orders
UPDATE paliad.deadline_rules SET rule_code = 'RoP.237.2', legal_source = 'UPC.RoP.237.2', rule_codes = ARRAY['RoP.237.2']::text[]
 WHERE id = 'a00e51bb-bcb6-48d0-9aa5-2216e9480c5c' AND rule_code IS NULL;  -- Anschlussberufung Orders
UPDATE paliad.deadline_rules SET rule_code = 'RoP.097.1', legal_source = 'UPC.RoP.97.1', rule_codes = ARRAY['RoP.097.1']::text[]
 WHERE id = '0531b6ba-98cc-48f4-adb8-da8b7a7c3535' AND rule_code IS NULL;  -- Aufhebung EPA Einheitswirkung
UPDATE paliad.deadline_rules SET rule_code = 'RoP.037.4', legal_source = 'UPC.RoP.37.4', rule_codes = ARRAY['RoP.037.4']::text[]
 WHERE id = '6b6b967c-65fd-4172-9640-1ffff8a46704' AND rule_code IS NULL;  -- Verweisung Zentralkammer
UPDATE paliad.deadline_rules SET rule_code = 'RoP.109.5', legal_source = 'UPC.RoP.109.5', rule_codes = ARRAY['RoP.109.5']::text[]
 WHERE id = '8c682cff-3423-41d8-81ca-b5b461461682' AND rule_code IS NULL;  -- Dolmetscher own-cost
UPDATE paliad.deadline_rules SET rule_code = 'RoP.007.2', legal_source = 'UPC.RoP.7.2', rule_codes = ARRAY['RoP.007.2']::text[]
 WHERE id = '9ed513c1-68df-455e-810e-a5d8d7b85729' AND rule_code IS NULL;  -- Übersetzungen Schriftstücke

-- FLAG-I: negative-declaration track (3 rows, RoP.069 by analogy per m)
UPDATE paliad.deadline_rules SET rule_code = 'RoP.069', legal_source = 'UPC.RoP.69', rule_codes = ARRAY['RoP.069']::text[]
 WHERE id = '521bf607-1c69-4dc5-a09e-70339bbe4684' AND rule_code IS NULL;  -- Erwid neg. Feststellungsklage
UPDATE paliad.deadline_rules SET rule_code = 'RoP.069', legal_source = 'UPC.RoP.69', rule_codes = ARRAY['RoP.069']::text[]
 WHERE id = 'e887b1fb-83ff-4073-b81b-c10dde6dc2c6' AND rule_code IS NULL;  -- Replik neg. Feststellung
UPDATE paliad.deadline_rules SET rule_code = 'RoP.069', legal_source = 'UPC.RoP.69', rule_codes = ARRAY['RoP.069']::text[]
 WHERE id = '0cf1d755-3ba5-44ce-87ca-f98bb076c995' AND rule_code IS NULL;  -- Duplik neg. Feststellung

-- RoP.271.b — 10-day service deferral on UPC initial submissions.
-- Set rule_codes[] to [primary substantive cite, 'RoP.271.b'] for the
-- 5 UPC initial-submission rows whose § 2 UPDATEs above only set
-- rule_code + legal_source. Idempotent via the IS DISTINCT FROM guard
-- — re-running matches no rows.
UPDATE paliad.deadline_rules
   SET rule_codes = target.rule_codes
  FROM (VALUES
        ('42be6c9b-8e84-4804-962f-94c3315aca1b'::uuid, ARRAY['RoP.013.1', 'RoP.271.b']::text[]),  -- inf.soc
        ('995c108e-e73a-4f9c-b79f-47abe7c94108'::uuid, ARRAY['RoP.042',   'RoP.271.b']::text[]),  -- rev.app
        ('ed0194b7-74ab-4402-8971-7211f6036ff9'::uuid, ARRAY['RoP.206',   'RoP.271.b']::text[]),  -- pi.app
        ('3e1719e8-f6f6-4260-8f02-754bd214937f'::uuid, ARRAY['RoP.131',   'RoP.271.b']::text[]),  -- damages.app
        ('eb1fa1d1-b345-42ba-ab14-79f5284166b0'::uuid, ARRAY['RoP.141',   'RoP.271.b']::text[])   -- disc.app
       ) AS target(id, rule_codes)
 WHERE paliad.deadline_rules.id = target.id
   AND paliad.deadline_rules.rule_codes IS DISTINCT FROM target.rule_codes;

-- =============================================================================
-- 10. § 10 R.19 label rename (inf.prelim / rev.prelim). Defensive
--     idempotent backstop for fermi's live prod write. Matches no rows
--     on the current prod DB (fermi already renamed) and on the first
--     post-mig fresh-deploy too. Catches any future prod that hasn't
--     seen the live write.
-- =============================================================================

UPDATE paliad.deadline_rules
   SET name = 'Einspruch (R. 19 VerfO)', rule_code = 'RoP.019.1'
 WHERE code = 'inf.prelim' AND name LIKE 'Vorab-Einrede%';

UPDATE paliad.deadline_rules
   SET name = 'Einspruch (R. 19 i.V.m. R. 46 VerfO)', rule_code = 'RoP.019.1'
 WHERE code = 'rev.prelim' AND name LIKE 'Vorab-Einrede%';

-- =============================================================================
-- 11. § 11 Side-fix: normalize the one un-padded UPC RoP <100 rule_code
--     outlier. legal_source stays 'UPC.RoP.49.1' (structured locator
--     never pads — convention § 0.2 of the proposal doc).
-- =============================================================================

UPDATE paliad.deadline_rules
   SET rule_code = 'RoP.049.1'
 WHERE rule_code = 'RoP.49.1'
   AND code      = 'rev.defence';

-- =============================================================================
-- 12. Refresh the deadline_search materialized view so search hits
--     return the newly populated rule_code + legal_source values.
-- =============================================================================

REFRESH MATERIALIZED VIEW paliad.deadline_search;

-- =============================================================================
-- 13. Hard assertions. Verifies the post-state matches the plan.
--
--     a) 11 active+published rows remain rule_code IS NULL: the 3
--        FLAG-J rows (m picks them up via /admin/rules) plus the 8
--        rows whose dedup decision is deferred (Mängelbeseitigung 6×
--        + Beginn-Hauptsache 2×).
--     b) No un-padded RoP.49.1 outlier remains.
--     c) Padded RoP.049.1 present at least twice (rev.defence
--        normalized + a32dcec1 orphan filled).
--     d) Each of the 3 clean-dedup sets has exactly 1 active+published
--        row after the archive flips.
-- =============================================================================

DO $$
DECLARE
    v_null_after  integer;
    v_old_outlier integer;
    v_new_padded  integer;
    v_dup_count   integer;
BEGIN
    -- (a) 3 FLAG-J + 8 deferred-dedup rows stay NULL.
    SELECT count(*) INTO v_null_after
      FROM paliad.deadline_rules
     WHERE rule_code IS NULL
       AND is_active = true
       AND lifecycle_state = 'published';
    IF v_null_after <> 11 THEN
        RAISE EXCEPTION
            'mig 097: expected 11 rule_code IS NULL active+published rows after backfill (3 FLAG-J + 8 deferred dedup), got %',
            v_null_after;
    END IF;

    -- (b) RoP.49.1 outlier normalized.
    SELECT count(*) INTO v_old_outlier
      FROM paliad.deadline_rules
     WHERE rule_code = 'RoP.49.1';
    IF v_old_outlier <> 0 THEN
        RAISE EXCEPTION
            'mig 097: expected 0 RoP.49.1 rows after normalization, got %',
            v_old_outlier;
    END IF;

    -- (c) RoP.049.1 present at least twice.
    SELECT count(*) INTO v_new_padded
      FROM paliad.deadline_rules
     WHERE rule_code = 'RoP.049.1';
    IF v_new_padded < 2 THEN
        RAISE EXCEPTION
            'mig 097: expected >= 2 RoP.049.1 rows after normalization + orphan fill, got %',
            v_new_padded;
    END IF;

    -- (d) Each clean-dedup set has exactly 1 active+published row.

    SELECT count(*) INTO v_dup_count
      FROM paliad.deadline_rules
     WHERE is_active = true
       AND lifecycle_state = 'published'
       AND id IN (
            'b588fa64-a727-4cfb-a45d-69a835a3b05a',
            'c24d494c-0da1-4f01-aa74-0f37f99fe1ae'
       );
    IF v_dup_count <> 1 THEN
        RAISE EXCEPTION
            'mig 097 dedup: Wiedereinsetzung-§123-PatG set must have 1 active+published row, got %',
            v_dup_count;
    END IF;

    SELECT count(*) INTO v_dup_count
      FROM paliad.deadline_rules
     WHERE is_active = true
       AND lifecycle_state = 'published'
       AND id IN (
            '1dfba5b1-4ed1-40c1-9cf6-4ed8ff7a0818',
            '5c0508f4-020a-4ef5-bcc7-1ee85eafe0b3'
       );
    IF v_dup_count <> 1 THEN
        RAISE EXCEPTION
            'mig 097 dedup: Berufungsschrift set must have 1 active+published row, got %',
            v_dup_count;
    END IF;

    SELECT count(*) INTO v_dup_count
      FROM paliad.deadline_rules
     WHERE is_active = true
       AND lifecycle_state = 'published'
       AND id IN (
            '573df3d1-8ea2-4a6e-b0d4-fc3cd10506da',
            '791fd0f7-a448-4711-b1aa-63e6df1e7c57'
       );
    IF v_dup_count <> 1 THEN
        RAISE EXCEPTION
            'mig 097 dedup: Berufungsbegründung set must have 1 active+published row, got %',
            v_dup_count;
    END IF;
END $$;
