-- CLEANUP SCRIPT FOR DUPLICATE USERS
-- IMPORTANT: Review each section carefully before executing
-- Always backup your database before running cleanup operations

-- Step 1: Identify which users to keep vs remove
-- Rule: Keep the user with more recent activity, Slack integration, or task assignments

-- EXAMPLE CLEANUPS (REVIEW AND MODIFY AS NEEDED):

-- For Gaurang Khoraba duplicates:
-- Keep: gaurang.khoraba@qrolic.com (appears to be the official email)
-- Remove: gaurang@qrolic.com (seems like a shorter/test email)

-- For Maulik Bhalodiya duplicates: 
-- Keep: maulik.bhalodiya@qrolic.com (official email format)
-- Remove: maulik.bhalodiya1@qrolic.com (has "1" appended, likely duplicate)

-- For Nilesh Kanzariya duplicates:
-- Keep: nilesh.kanzariya@qrolic.com (official email format)  
-- Remove: nilesh.kanzariya1@qrolic.com (has "1" appended, likely duplicate)

-- BEFORE DELETING: Transfer any important data from duplicate users to the main user

-- Step 2: Transfer task assignments (if needed)
-- UPDATE flochief_tasks SET assigned_to = ? WHERE assigned_to = ?;
-- UPDATE flochief_tasks SET created_by = ? WHERE created_by = ?;

-- Step 3: Transfer project assignments (if needed)  
-- UPDATE user_project_assignments SET user_id = ? WHERE user_id = ?;

-- Step 4: Transfer work sessions (if needed)
-- UPDATE work_sessions SET user_id = ? WHERE user_id = ?;

-- Step 5: Transfer work events (if needed)
-- UPDATE work_events SET user_id = ? WHERE user_id = ?;

-- Step 6: CAREFULLY delete duplicate users (ONLY AFTER TRANSFERRING DATA)
-- DELETE FROM users WHERE id = ? AND email = 'duplicate_email@domain.com';

-- SAFER APPROACH: Instead of deleting, deactivate duplicates first
-- UPDATE users SET is_active = 0, email = CONCAT(email, '_DUPLICATE_', id) WHERE id IN (?, ?, ?);

-- Test query to verify cleanup worked:
-- SELECT COUNT(*) as total_active_users FROM users WHERE organization_id = 11 AND is_active = 1;