-- =============================================================
-- V022: Fix incorrectly oriented player_links for Discord↔Telegram
-- =============================================================
-- Scope: Reverse player_links where Discord was incorrectly made primary
-- and Telegram secondary. Telegram (with historical group/session data)
-- must always be the primary account.
-- =============================================================

UPDATE player_links pl
SET primary_player_id   = pl.secondary_player_id,
    secondary_player_id = pl.primary_player_id
FROM players p1, players p2
WHERE pl.primary_player_id = p1.id
  AND pl.secondary_player_id = p2.id
  AND p1.platform = 'Discord'
  AND p2.platform = 'Telegram';
