-- t-paliad-165 follow-up (m's 2026-05-08 22:08 dogfood): add jurisdiction
-- to paliad.deadline_concept_event_types so DE rules don't auto-fill a
-- UPC event_type and vice versa.
--
-- Bug being fixed
-- ---------------
-- m: rule '§ 276 Abs. 1 S. 2 ZPO — Klageerwiderung' (DE) auto-filled to
-- 'Klageerwiderung' but the chosen event_type was upc_statement_of_defence
-- (UPC). Both render as 'Klageerwiderung' in the UI, but they are
-- different legal events in different jurisdictions — the auto-link is
-- technically wrong even though the label looks right.
--
-- Root cause
-- ----------
-- Migration 073 made the junction one-default-per-concept. The same
-- legal concept ('statement-of-defence' = Klageerwiderung) has several
-- jurisdictional flavours (upc_statement_of_defence, de_klageerwiderung,
-- DPMA Erwiderung, EPA Patentinhaber-Erwiderung). The default was
-- jurisdiction-blind — it always picked the UPC variant.
--
-- Fix
-- ---
-- 1. Add a jurisdiction text column to the junction.
-- 2. Backfill from each event_type's own jurisdiction.
-- 3. Replace the unique-default index with a (concept_id, jurisdiction)
--    pair so each concept can carry one default per jurisdiction.
-- 4. Add jurisdictional defaults where a non-UPC event_type genuinely
--    exists (DE Klageerwiderung, DPMA / EPO opposition + appeal).
--
-- Lookup contract (consumed by the rule-service hydrator)
-- -------------------------------------------------------
-- For a rule with proceeding_types.jurisdiction = J, the auto-fill
-- looks up the row WHERE is_default AND jurisdiction = J. EPA→EPO
-- canonicalisation lives in the Go service (proceeding_types use 'EPA'
-- but event_types use 'EPO' — the two columns disagreed before this
-- mapping table existed). When NO row matches the rule's jurisdiction,
-- the auto-fill silently no-ops; better than a wrong default.

ALTER TABLE paliad.deadline_concept_event_types
    ADD COLUMN IF NOT EXISTS jurisdiction text;

COMMENT ON COLUMN paliad.deadline_concept_event_types.jurisdiction IS
    'Which jurisdiction this default applies to. Matches the rule''s '
    'proceeding_types.jurisdiction (UPC / DE / DPMA / EPO). EPA→EPO '
    'canonicalisation is done service-side. NULL = applies to any '
    'jurisdiction (the catch-all fallback — currently unused).';

-- Backfill jurisdiction from the event_type's own column.
UPDATE paliad.deadline_concept_event_types j
   SET jurisdiction = et.jurisdiction
  FROM paliad.event_types et
 WHERE j.event_type_id = et.id
   AND j.jurisdiction IS NULL;

-- Replace the old unique-default index (one default per concept) with
-- one default per (concept, jurisdiction). We DROP IF EXISTS so the
-- migration is rerunnable against a freshly-rebuilt schema.
DROP INDEX IF EXISTS paliad.deadline_concept_event_types_one_default;

CREATE UNIQUE INDEX IF NOT EXISTS deadline_concept_event_types_one_default_per_jur
    ON paliad.deadline_concept_event_types (concept_id, jurisdiction)
    WHERE is_default = true;

-- ============================================================================
-- Demote then re-elect defaults so each (concept, jurisdiction) pair is
-- correctly anchored. Rows that never had jurisdiction picked stay as
-- non-defaults until a curated row beats them.
-- ============================================================================

-- statement-of-defence DE: de_klageerwiderung becomes the DE default.
INSERT INTO paliad.deadline_concept_event_types (concept_id, event_type_id, is_default, sort_order, jurisdiction)
SELECT dc.id, et.id, true, 10, 'DE'
  FROM paliad.deadline_concepts dc
  JOIN paliad.event_types et ON et.slug = 'de_klageerwiderung' AND et.archived_at IS NULL
 WHERE dc.slug = 'statement-of-defence'
ON CONFLICT (concept_id, event_type_id) DO UPDATE
   SET is_default = true, jurisdiction = 'DE', sort_order = 10;

-- opposition: split per jurisdiction. EPO is the canonical EU-wide
-- pre-grant Einspruch, DPMA is the German national variant.
UPDATE paliad.deadline_concept_event_types j
   SET is_default = true, jurisdiction = 'EPO', sort_order = 10
  FROM paliad.deadline_concepts dc, paliad.event_types et
 WHERE j.concept_id = dc.id
   AND j.event_type_id = et.id
   AND dc.slug = 'opposition'
   AND et.slug = 'epo_opposition_filing';

UPDATE paliad.deadline_concept_event_types j
   SET is_default = true, jurisdiction = 'DPMA', sort_order = 20
  FROM paliad.deadline_concepts dc, paliad.event_types et
 WHERE j.concept_id = dc.id
   AND j.event_type_id = et.id
   AND dc.slug = 'opposition'
   AND et.slug = 'dpma_opposition';

-- request-for-examination: DPMA is the only jurisdiction with an
-- event_type counterpart (EP-grant exam request has no event_type yet).
UPDATE paliad.deadline_concept_event_types j
   SET is_default = true, jurisdiction = 'DPMA'
  FROM paliad.deadline_concepts dc, paliad.event_types et
 WHERE j.concept_id = dc.id
   AND j.event_type_id = et.id
   AND dc.slug = 'request-for-examination'
   AND et.slug = 'dpma_examination_request';

-- notice-of-appeal: keep the UPC default (already set by mig 073) AND
-- add EPO + DPMA jurisdictional variants for non-UPC rules.
INSERT INTO paliad.deadline_concept_event_types (concept_id, event_type_id, is_default, sort_order, jurisdiction)
SELECT dc.id, et.id, true, 10, 'EPO'
  FROM paliad.deadline_concepts dc
  JOIN paliad.event_types et ON et.slug = 'epo_appeal_notice' AND et.archived_at IS NULL
 WHERE dc.slug = 'notice-of-appeal'
ON CONFLICT (concept_id, event_type_id) DO UPDATE
   SET is_default = true, jurisdiction = 'EPO', sort_order = 10;

INSERT INTO paliad.deadline_concept_event_types (concept_id, event_type_id, is_default, sort_order, jurisdiction)
SELECT dc.id, et.id, true, 10, 'DPMA'
  FROM paliad.deadline_concepts dc
  JOIN paliad.event_types et ON et.slug = 'dpma_appeal' AND et.archived_at IS NULL
 WHERE dc.slug = 'notice-of-appeal'
ON CONFLICT (concept_id, event_type_id) DO UPDATE
   SET is_default = true, jurisdiction = 'DPMA', sort_order = 10;

-- statement-of-grounds-of-appeal: keep UPC default; add EPO variant.
UPDATE paliad.deadline_concept_event_types j
   SET is_default = true, jurisdiction = 'EPO', sort_order = 10
  FROM paliad.deadline_concepts dc, paliad.event_types et
 WHERE j.concept_id = dc.id
   AND j.event_type_id = et.id
   AND dc.slug = 'statement-of-grounds-of-appeal'
   AND et.slug = 'epo_appeal_grounds';

-- ============================================================================
-- Final pass: any junction row that still has NULL jurisdiction (none
-- expected after the backfill, but defensive) gets its event_type's
-- jurisdiction copied so the partial-unique index is well-defined.
-- ============================================================================
UPDATE paliad.deadline_concept_event_types j
   SET jurisdiction = et.jurisdiction
  FROM paliad.event_types et
 WHERE j.event_type_id = et.id
   AND j.jurisdiction IS NULL;
