-- Verify auto-closed sessions now have midnight stored correctly
SELECT
    id,
    user_id,
    session_date,
    clock_in_time,
    clock_out_time,
    session_status,
    closed_by_type,
    close_reason
FROM work_sessions
WHERE closed_by_type = 'auto'
ORDER BY session_date DESC, id DESC
LIMIT 50;

-- Optional one-time cleanup for already auto-closed rows missing a clock_out_time
UPDATE work_sessions
SET
    clock_out_time = DATE_ADD(session_date, INTERVAL 1 DAY),
    close_reason = CASE
        WHEN close_reason IS NULL OR close_reason = '' THEN 'Auto-closed at midnight'
        ELSE close_reason
    END
WHERE closed_by_type = 'auto'
  AND session_status = 'CLOSED'
  AND (clock_out_time IS NULL OR clock_out_time = '0000-00-00 00:00:00');
