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


$passengerId = filterRequest("passengerId"); 

$sql = "SELECT
    r.id AS id,
    r.start_location,
    r.end_location,
    r.date,
    r.price,
    r.status,
    r.paymentMethod,
    r.distance,
    r.carType,
    r.rideTimeFinish,
    r.rideTimeStart,
    r.DriverIsGoingToPassenger,
    COUNT(rp.id) AS countRateFromPassengerToDrivers,
    COUNT(rd.id) AS countRateFromDriverToPassengers,
    MAX(rp.rating) AS rateFromPassengerToDriver,
    MAX(rd.rating) AS rateFromDriversToPassengers,
    MAX(rp.comment) AS commentFromPassengerToDriver,
    MAX(rd.comment) AS commentFromDriverToPassenger
FROM
    ride r
LEFT JOIN ratingPassenger rp ON
    rp.passenger_id = r.passenger_id
LEFT JOIN ratingDriver rd ON
    rd.driver_id = r.driver_id
WHERE
    r.passenger_id = :passengerId
GROUP BY
    r.id,
    r.start_location,
    r.end_location,
    r.date,
    r.price,
    r.status,
    r.paymentMethod,
    r.distance,
    r.carType,
    r.rideTimeFinish,
    r.rideTimeStart,
    r.DriverIsGoingToPassenger
ORDER BY
    r.date
DESC
LIMIT 1;
    ";

$stmt = $con->prepare($sql);
$stmt->bindParam(':passengerId', $passengerId, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

if ($result) {
    // Print all promo records
    jsonSuccess($result);
} else {
    // Print an empty list
    jsonSuccess([]);
}
?>