-- SIMPLE POSTGRESQL FIX
-- Just copy and run this entire script

-- Fix Cursor activities
UPDATE activity_records 
SET project_name = 
    CASE 
        WHEN window_title LIKE '% - % - Cursor' THEN
            TRIM(SPLIT_PART(REPLACE(window_title, ' - Cursor', ''), ' - ', -1))
        WHEN window_title LIKE '% - Cursor' THEN
            TRIM(REPLACE(window_title, ' - Cursor', ''))
        ELSE project_name
    END
WHERE application_name = 'Cursor.exe'
AND developer_id = 'ankita_gholap';

-- Fix VS Code activities  
UPDATE activity_records 
SET project_name = 
    CASE
        WHEN window_title LIKE '% - % - Visual Studio Code' THEN
            TRIM(SPLIT_PART(REPLACE(window_title, ' - Visual Studio Code', ''), ' - ', -1))
        WHEN window_title LIKE '% - Visual Studio Code' THEN
            TRIM(REPLACE(window_title, ' - Visual Studio Code', ''))
        ELSE project_name
    END
WHERE application_name = 'Code.exe'
AND developer_id = 'ankita_gholap';

-- Merge timesheet variations
UPDATE activity_records
SET project_name = 'timesheet'
WHERE project_name IN ('timesheet_new', 'timesheet_New', 'timesheet_backup');

-- Check results
SELECT 
    project_name,
    COUNT(*) as activities,
    SUM(duration)/1000/60 as total_minutes
FROM activity_records
WHERE developer_id = 'ankita_gholap'
AND DATE(timestamp) = CURRENT_DATE
AND application_name IN ('Cursor.exe', 'Code.exe')
GROUP BY project_name
ORDER BY total_minutes DESC;
