-- Deduplicate users with the same name (case-insensitive) per event,
-- keeping the oldest account so no real data is lost.
DELETE FROM "user"
WHERE id NOT IN (
    SELECT DISTINCT ON (event_id, LOWER(display_name)) id
    FROM "user"
    ORDER BY event_id, LOWER(display_name), created_at ASC
);

-- Drop the old non-unique index (replaced below)
DROP INDEX IF EXISTS idx_user_event_name;

-- Unique index enforces one account per name per event (case-insensitive)
CREATE UNIQUE INDEX idx_user_event_name_ci
    ON "user" (event_id, LOWER(display_name));
