-- ══════════════════════════════════════════════════════════
-- CRITICAL FIX: plan_id duplicates caused wrong package activation
-- ══════════════════════════════════════════════════════════

-- Step 1: Find and show duplicates (run this first to see the problem)
-- SELECT plan_id, GROUP_CONCAT(id,':',name) as packages, COUNT(*) as cnt
-- FROM net_packages GROUP BY plan_id HAVING cnt > 1;

-- Step 2: Auto-fix duplicates by renumbering plan_id sequentially
SET @rownum = 0;
UPDATE net_packages
JOIN (
  SELECT id, (@rownum := @rownum + 1) as new_plan_id
  FROM net_packages
  ORDER BY id
) ranked ON net_packages.id = ranked.id
SET net_packages.plan_id = ranked.new_plan_id;

-- Step 3: Prevent future duplicates at the database level
ALTER TABLE net_packages ADD UNIQUE KEY `uniq_plan_id` (`plan_id`);
