-- t-paliad-207 — re-sequence upc.inf.cfi rules so within any tied-date
-- group the infringement-track responses sit ABOVE the revocation-
-- track responses ABOVE the amendment-track responses. m's ask
-- 2026-05-18 18:08: "the infringement parts (like Replik) should show
-- above the part for the revocation (Erwiderung Nichtigkeitswider-
-- klage)".
--
-- Three tracks coexist on upc.inf.cfi once the with_ccr / with_amend
-- flags are set. They share calendar dates because R.29 / R.30 / R.32
-- all key off the SoD or its descendants. The current sequence_orders
-- (post-mig 100) interleave them; the user sees Erwiderung-zur-CCR
-- before Replik even though Replik is the infringement-side response
-- to the same triggering event.
--
-- New sequence_order assignment (preserves the soc=0, prelim=5,
-- sod=10, ccr=11 anchors at the head; phase markers interim/oral/
-- decision/cost_app/appeal_spawn keep their existing 40/50/60/70/80
-- slots at the tail):
--
--   Old → New   submission_code             track         date
--   ---   ---   ---------------             -----         ----
--     0     0   upc.inf.cfi.soc             —             D+0
--     5     5   upc.inf.cfi.prelim          —             D+1mo
--    10    10   upc.inf.cfi.sod             infringement  D+3mo
--    11    20   upc.inf.cfi.ccr             revocation    D+3mo
--    20    12   upc.inf.cfi.reply           infringement  D+5mo  ← MOVED UP
--    12    22   upc.inf.cfi.def_to_ccr      revocation    D+5mo
--    13    30   upc.inf.cfi.app_to_amend    amendment     D+5mo
--    30    14   upc.inf.cfi.rejoin          infringement  D+6mo  ← MOVED UP
--    22    24   upc.inf.cfi.reply_def_ccr   revocation    D+7mo
--    21    32   upc.inf.cfi.def_to_amend    amendment     D+7mo
--    32    26   upc.inf.cfi.rejoin_reply_ccr revocation    D+8mo
--    31    34   upc.inf.cfi.reply_def_amd   amendment     D+8mo
--    33    36   upc.inf.cfi.rejoin_amd      amendment     D+9mo
--    40    40   upc.inf.cfi.interim         phase         later
--    50    50   upc.inf.cfi.oral            phase         later
--    60    60   upc.inf.cfi.decision        phase         later
--    70    70   upc.inf.cfi.cost_app        phase         later
--    80    80   upc.inf.cfi.appeal_spawn    phase         later
--
-- Order within each tied-date group after the reshuffle:
--   D+3mo: sod(10), ccr(20)                                — SoD then its CCR
--   D+5mo: reply(12), def_to_ccr(22), app_to_amend(30)    — inf → rev → amd
--   D+7mo: reply_def_ccr(24), def_to_amend(32)            — rev → amd
--   D+8mo: rejoin_reply_ccr(26), reply_def_amd(34)        — rev → amd
--
-- (no infringement-track rule at +7mo or +8mo so revocation leads
-- those dates; rejoin sits alone at +6mo so it has no peers to order
-- against.)
--
-- audit_reason set_config required at the top — the deadline_rules
-- audit trigger raises EXCEPTION 'audit reason required' on any
-- mutation without it (cf. mig 099 hotfix history).
--
-- Idempotency: every UPDATE is guarded by both the submission_code
-- AND the SOURCE sequence_order, so re-apply is a no-op once the new
-- numbers are in place.

SELECT set_config(
    'paliad.audit_reason',
    'mig 105: re-sequence upc.inf.cfi rules track-aware (infringement → revocation → amendment within tied-date groups; m''s 2026-05-18 ask, t-paliad-207 interactive session)',
    true);

-- Two-phase swap to avoid sequence collisions during the UPDATE
-- (otherwise two rules can briefly share a sequence_order if Postgres
-- evaluates them in parallel). Phase 1: move every reshuffled rule to
-- a high temporary number (1000+). Phase 2: assign final numbers.

-- ─── Phase 1: park reshuffled rules at 1000+ ────────────────────────

UPDATE paliad.deadline_rules
   SET sequence_order = 1011
 WHERE submission_code = 'upc.inf.cfi.ccr'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 11;

UPDATE paliad.deadline_rules
   SET sequence_order = 1012
 WHERE submission_code = 'upc.inf.cfi.def_to_ccr'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 12;

UPDATE paliad.deadline_rules
   SET sequence_order = 1013
 WHERE submission_code = 'upc.inf.cfi.app_to_amend'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 13;

UPDATE paliad.deadline_rules
   SET sequence_order = 1020
 WHERE submission_code = 'upc.inf.cfi.reply'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 20;

UPDATE paliad.deadline_rules
   SET sequence_order = 1021
 WHERE submission_code = 'upc.inf.cfi.def_to_amend'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 21;

UPDATE paliad.deadline_rules
   SET sequence_order = 1022
 WHERE submission_code = 'upc.inf.cfi.reply_def_ccr'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 22;

UPDATE paliad.deadline_rules
   SET sequence_order = 1030
 WHERE submission_code = 'upc.inf.cfi.rejoin'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 30;

UPDATE paliad.deadline_rules
   SET sequence_order = 1031
 WHERE submission_code = 'upc.inf.cfi.reply_def_amd'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 31;

UPDATE paliad.deadline_rules
   SET sequence_order = 1032
 WHERE submission_code = 'upc.inf.cfi.rejoin_reply_ccr'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 32;

UPDATE paliad.deadline_rules
   SET sequence_order = 1033
 WHERE submission_code = 'upc.inf.cfi.rejoin_amd'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 33;

-- ─── Phase 2: assign final track-aware numbers ──────────────────────

UPDATE paliad.deadline_rules
   SET sequence_order = 12
 WHERE submission_code = 'upc.inf.cfi.reply'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 1020;

UPDATE paliad.deadline_rules
   SET sequence_order = 14
 WHERE submission_code = 'upc.inf.cfi.rejoin'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 1030;

UPDATE paliad.deadline_rules
   SET sequence_order = 20
 WHERE submission_code = 'upc.inf.cfi.ccr'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 1011;

UPDATE paliad.deadline_rules
   SET sequence_order = 22
 WHERE submission_code = 'upc.inf.cfi.def_to_ccr'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 1012;

UPDATE paliad.deadline_rules
   SET sequence_order = 24
 WHERE submission_code = 'upc.inf.cfi.reply_def_ccr'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 1022;

UPDATE paliad.deadline_rules
   SET sequence_order = 26
 WHERE submission_code = 'upc.inf.cfi.rejoin_reply_ccr'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 1032;

UPDATE paliad.deadline_rules
   SET sequence_order = 30
 WHERE submission_code = 'upc.inf.cfi.app_to_amend'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 1013;

UPDATE paliad.deadline_rules
   SET sequence_order = 32
 WHERE submission_code = 'upc.inf.cfi.def_to_amend'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 1021;

UPDATE paliad.deadline_rules
   SET sequence_order = 34
 WHERE submission_code = 'upc.inf.cfi.reply_def_amd'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 1031;

UPDATE paliad.deadline_rules
   SET sequence_order = 36
 WHERE submission_code = 'upc.inf.cfi.rejoin_amd'
   AND proceeding_type_id = 8
   AND lifecycle_state = 'published'
   AND sequence_order = 1033;
