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

$driver_id = filterRequest("driver_id");

try {
    $sql = "
        SELECT 
            DATE(r.created_at) as day,
            COUNT(r.id) as trips,
            COALESCE(SUM(r.price_for_driver), 0) as earnings,
            COALESCE(SUM(TIMESTAMPDIFF(MINUTE, r.time, r.endtime))/60, 0) as hours
        FROM `ride` r
        WHERE r.driver_id = :driver_id 
          AND r.status = 'Finished'
          AND r.created_at >= DATE(NOW()) - INTERVAL 6 DAY
        GROUP BY DATE(r.created_at)
        ORDER BY DATE(r.created_at) ASC
    ";

    $stmt = $con->prepare($sql);
    $stmt->bindParam(':driver_id', $driver_id, PDO::PARAM_INT);
    $stmt->execute();

    if ($stmt->rowCount() > 0) {
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
        jsonSuccess($rows);
    } else {
        jsonSuccess([]);
    }
} catch (PDOException $e) {
    // Return empty but log error
    error_log("getWeeklyAggregate Error: " . $e->getMessage());
    jsonError("Database error occurred");
}
?>
