-- 137_proceeding_role_labels — t-paliad-301, m/paliad#132
--
-- Bug A fix: per-proceeding role labels so the Verfahrensablauf side
-- selector can render "Berufungskläger / Berufungsbeklagter" for the
-- unified UPC Berufung tile instead of the generic "Klägerseite /
-- Beklagtenseite".
--
-- Four new optional columns on paliad.proceeding_types. NULL on a
-- column falls back to the language-default ("Klägerseite" / "Claimant
-- side" / "Beklagtenseite" / "Defendant side") in the frontend renderer.
-- Only the proceedings whose role-naming actually differs get a backfill.
--
-- Live-DB audit (mcp__supabase__execute_sql) before drafting:
--   - paliad.proceeding_types has 14 columns; the 4 target columns do
--     NOT exist (zero name collisions).
--   - Zero triggers on paliad.proceeding_types. No audit_reason
--     setup needed.
--   - No updated_at / created_at on the table — DO NOT include
--     timestamp UPDATEs (lesson from mig 134 HOTFIX 3).
--
-- ADDITIVE ONLY. ALTER + UPDATE statements; no CHECK constraints
-- (the columns are free-text labels, validated at the application layer).
-- Down migration drops the 4 columns.
--
-- See m/paliad#132 for the full design rationale + the role-label
-- matrix per proceeding code.

-- ---------------------------------------------------------------
-- 1. Schema additions
-- ---------------------------------------------------------------

ALTER TABLE paliad.proceeding_types
    ADD COLUMN role_proactive_label_de text NULL;

ALTER TABLE paliad.proceeding_types
    ADD COLUMN role_proactive_label_en text NULL;

ALTER TABLE paliad.proceeding_types
    ADD COLUMN role_reactive_label_de text NULL;

ALTER TABLE paliad.proceeding_types
    ADD COLUMN role_reactive_label_en text NULL;

COMMENT ON COLUMN paliad.proceeding_types.role_proactive_label_de IS
    'DE label for the proactive (claimant-equivalent) side of this '
    'proceeding. NULL = renderer falls back to "Klägerseite". '
    't-paliad-301 / m/paliad#132 Bug A.';
COMMENT ON COLUMN paliad.proceeding_types.role_proactive_label_en IS
    'EN label for the proactive side. NULL = "Claimant side".';
COMMENT ON COLUMN paliad.proceeding_types.role_reactive_label_de IS
    'DE label for the reactive (defendant-equivalent) side. NULL = '
    '"Beklagtenseite".';
COMMENT ON COLUMN paliad.proceeding_types.role_reactive_label_en IS
    'EN label for the reactive side. NULL = "Defendant side".';

-- ---------------------------------------------------------------
-- 2. Audit-first NOTICE pass.
--
-- Lists which proceeding_types are about to receive a backfill so
-- the operator sees the scope before the UPDATE fires. NULL columns
-- on every other row stay NULL (the frontend falls back to defaults).
-- ---------------------------------------------------------------

DO $$
DECLARE
    rec record;
    backfill_count int := 0;
BEGIN
    RAISE NOTICE '[mig 137] Proceedings that will receive role-label backfill:';
    FOR rec IN
        SELECT code, name
          FROM paliad.proceeding_types
         WHERE code IN ('upc.apl.unified', 'upc.rev.cfi', 'epa.opp.opd', 'epa.opp.boa')
         ORDER BY code
    LOOP
        RAISE NOTICE '[mig 137]   %  %', rec.code, rec.name;
        backfill_count := backfill_count + 1;
    END LOOP;
    RAISE NOTICE '[mig 137] Total: % proceedings (others stay NULL → renderer default)', backfill_count;
END $$;

-- ---------------------------------------------------------------
-- 3. Backfill.
--
-- Per the design matrix in m/paliad#132:
--   - upc.apl.unified → Berufungskläger / Berufungsbeklagter / Appellant / Appellee
--   - upc.rev.cfi     → Antragsteller (Nichtigkeit) / Antragsgegner (Nichtigkeit) /
--                       Revocation claimant / Revocation defendant
--   - epa.opp.opd     → Einsprechende(r) / Patentinhaber(in) /
--                       Opponent / Patentee
--   - epa.opp.boa     → Einsprechende(r) / Patentinhaber(in) /
--                       Opponent / Patentee
--   - (others)        → stay NULL → frontend defaults
-- ---------------------------------------------------------------

UPDATE paliad.proceeding_types
   SET role_proactive_label_de = 'Berufungskläger',
       role_reactive_label_de  = 'Berufungsbeklagter',
       role_proactive_label_en = 'Appellant',
       role_reactive_label_en  = 'Appellee'
 WHERE code = 'upc.apl.unified';

UPDATE paliad.proceeding_types
   SET role_proactive_label_de = 'Antragsteller (Nichtigkeit)',
       role_reactive_label_de  = 'Antragsgegner (Nichtigkeit)',
       role_proactive_label_en = 'Revocation claimant',
       role_reactive_label_en  = 'Revocation defendant'
 WHERE code = 'upc.rev.cfi';

UPDATE paliad.proceeding_types
   SET role_proactive_label_de = 'Einsprechende(r)',
       role_reactive_label_de  = 'Patentinhaber(in)',
       role_proactive_label_en = 'Opponent',
       role_reactive_label_en  = 'Patentee'
 WHERE code IN ('epa.opp.opd', 'epa.opp.boa');

-- ---------------------------------------------------------------
-- 4. Post-migration NOTICE — informational only.
-- ---------------------------------------------------------------

DO $$
DECLARE
    rec record;
BEGIN
    RAISE NOTICE '[mig 137] post: backfilled role-label distribution:';
    FOR rec IN
        SELECT code,
               role_proactive_label_de,
               role_reactive_label_de
          FROM paliad.proceeding_types
         WHERE role_proactive_label_de IS NOT NULL
         ORDER BY code
    LOOP
        RAISE NOTICE '[mig 137]   %  proactive=%  reactive=%',
            rec.code, rec.role_proactive_label_de, rec.role_reactive_label_de;
    END LOOP;
END $$;
