<?php
/**
 * Replace the UPDATE section in api/auto-close-sessions.php with this pattern.
 * This stores next-day midnight in MySQL and marks the session clearly as auto-closed.
 */

$updateSql = "
    UPDATE work_sessions
    SET
        clock_out_time = DATE_ADD(session_date, INTERVAL 1 DAY),
        session_status = 'CLOSED',
        closed_by_type = 'auto',
        closed_by_id = NULL,
        close_reason = 'Auto-closed at midnight',
        updated_at = NOW()
    WHERE session_status = 'OPEN'
      AND (clock_out_time IS NULL OR clock_out_time = '0000-00-00 00:00:00')
      AND session_date < CURDATE()
";

$result = mysqli_query($conn, $updateSql);
if (!$result) {
    error_log('auto-close-sessions.php SQL error: ' . mysqli_error($conn));
}
