<?php
require_once __DIR__ . '/../../connect.php';
$driverID = filterRequest("driverID");

$sql = "SELECT
  p1.id,
  p1.amount,
  p2.total_amount,
  p1.payment_method,
  p1.isGiven,
  p1.passengerID,
  p1.rideId,
  p1.driverID,
  (
    SELECT SUM(amount)
    FROM payments
    WHERE driverID = '$driverID'
      AND DATE(created_at) = CURDATE()
  ) AS todayAmount,
  p1.created_at,
  p1.updated_at,
  (
    SELECT ROUND(AVG(CAST(rating AS DECIMAL(4,2))), 2)
    FROM ratingDriver
    WHERE driver_id = '$driverID'
  ) AS rating
FROM payments p1
JOIN (
  SELECT driverID, SUM(amount) AS total_amount
  FROM payments
  WHERE isGiven = 'waiting'
  GROUP BY driverID
) p2 ON p1.driverID = p2.driverID
WHERE p1.isGiven = 'waiting'
  AND p1.driverID = '$driverID'
  AND DATE(p1.created_at) = CURDATE(); ";
$stmt = $con->prepare($sql);
$stmt->execute();

if ($stmt->rowCount() > 0) {
    // Fetch the record
    $row = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $count = $stmt->rowCount();
    
    // $response = array(
       
    //     "message" => "Payment data saved successfully",
    //     "id" => "0",
    //     "count" => $count,
    //     "data" => $rows
    // );

    // echo json_encode($response);
    jsonSuccess($row);
    
}
    else{
    // Print a failure message
    jsonError($message = "No wallet record found");
}
?>