-- 156_trigger_event_id_partial_deprecation — t-paliad-331 / m/paliad#149 Phase 2 P4 (partial)
--
-- Partial deprecation step toward retiring paliad.trigger_events.
-- The full table-drop (and the route + service + 5 read-site removals
-- the design's §3.4 + §4.3 lay out) is gated on the editorial backfill
-- of the 73 orphan globals — sequencing_rules rows that carry
-- trigger_event_id NOT NULL AND proceeding_type_id IS NULL today. m
-- drives that walk via /admin/procedural-events at his cadence (no
-- coder time blocked); this mig prepares the way without breaking the
-- legacy route the orphans still depend on.
--
-- What this mig does (live-DB audited 2026-05-27 pre-flight):
--
--  1. NULL out the 2 hybrid rules that carry BOTH parent_id AND
--     trigger_event_id. Per design §2.1 / m's Q1: parent_id is the
--     canonical predecessor link; trigger_event_id on those 2 rows is
--     redundant. The parent_id chain keeps the live edge — no data
--     loss, no route disruption (the route only reads trigger_event_id
--     for the 73 orphan globals, which have no parent_id).
--
--  2. NOT-DROP the column or the table. Both stay live so the
--     /api/tools/event-deadlines route continues to serve the 73
--     orphan globals until editorial reparenting lands.
--
-- The full P4 (mig that DROPs paliad.trigger_events + the
-- `sequencing_rules.trigger_event_id` column + the legacy route +
-- EventDeadlineService + ExportService::1680 + cmd/gen-upc-snapshot/
-- main.go:185-202) lands AFTER the 73 orphans are reparented. Until
-- then, the legacy surface remains.

BEGIN;

SELECT set_config(
    'paliad.audit_reason',
    'mig 156: trigger_event_id partial deprecation — NULL out 2 hybrid rules (t-paliad-331 / m/paliad#149 Phase 2 P4 partial)',
    true
);

-- ----------------------------------------------------------------
-- 1. Snapshot the 2 hybrid rows for audit + rollback.
-- ----------------------------------------------------------------

CREATE TABLE paliad.sequencing_rules_pre_156 AS
SELECT * FROM paliad.sequencing_rules
 WHERE trigger_event_id IS NOT NULL
   AND parent_id IS NOT NULL
   AND is_active = true;

COMMENT ON TABLE paliad.sequencing_rules_pre_156 IS
    'Snapshot of the 2 hybrid rules (trigger_event_id NOT NULL AND '
    'parent_id NOT NULL) taken in the same TX as mig 156, before their '
    'trigger_event_id is NULL''ed. Rollback aid until P4 final lands.';

-- ----------------------------------------------------------------
-- 2. NULL out trigger_event_id on hybrid rules — parent_id is the
--    canonical predecessor link per design §2.1.
-- ----------------------------------------------------------------

UPDATE paliad.sequencing_rules
   SET trigger_event_id = NULL
 WHERE trigger_event_id IS NOT NULL
   AND parent_id IS NOT NULL
   AND is_active = true;

DO $$
DECLARE
    remaining_hybrids int;
BEGIN
    SELECT COUNT(*) INTO remaining_hybrids
      FROM paliad.sequencing_rules
     WHERE trigger_event_id IS NOT NULL
       AND parent_id IS NOT NULL
       AND is_active = true;
    IF remaining_hybrids <> 0 THEN
        RAISE EXCEPTION '[mig 156] expected 0 active hybrid rules, found %', remaining_hybrids;
    END IF;
    RAISE NOTICE '[mig 156] hybrid-rule cleanup OK — 0 active rules carry both parent_id and trigger_event_id';
END $$;

COMMIT;
