<?php
require_once __DIR__ . '/../../connect.php';

$driver_id = filterRequest("driver_id");

// Get the current date.
$current_date = date('Y-m-d');

// Create a SQL query to select the total duration for the driver today.
$sql = "SELECT
  SEC_TO_TIME(COUNT(*) * 60) AS total_duration
FROM
  car_tracks
WHERE
  car_tracks.driver_id = '$driver_id'
  AND car_tracks.created_at >= '$current_date'
  AND car_tracks.created_at < DATE_ADD('$current_date', INTERVAL 1 DAY);";

$stmt = $con->prepare($sql);
$stmt->execute();
$car_locations = $stmt->fetchAll(PDO::FETCH_ASSOC);

if ($car_locations) {
  // Print the car location data as JSON
  jsonSuccess($car_locations);
} else {
  // Print a failure message
  jsonError($message = "No car locations found");
}

?>
