<?php
/**
 * Replace the UPDATE section in cron/auto-close-sessions.php with this pattern.
 * Midnight must be stored as next day 00:00:00 in SQL, then displayed as 24h00 in the backoffice.
 */

$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('cron auto-close-sessions.php SQL error: ' . mysqli_error($conn));
}
