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

// استلام الرقم وتشفيره
$phone = filterRequest("phone");
$phoneEncrypted = $encryptionHelper->encryptData($phone);

$sql = "SELECT 
    p.*,
    COALESCE(r.id, 0) AS ride_id,
    COALESCE(r.start_location, '') AS start_location,
    COALESCE(r.end_location, '') AS end_location,
    COALESCE(r.date, '1970-01-01') AS ride_date,
    COALESCE(r.time, '00:00:00') AS ride_time,
    COALESCE(r.endtime, '00:00:00') AS ride_endtime,
    COALESCE(r.price, 0) AS price,
    COALESCE(r.passenger_id, 0) AS ride_passenger_id,
    COALESCE(r.driver_id, 0) AS driver_id,
    COALESCE(r.status, '') AS ride_status,
    COALESCE(r.paymentMethod, '') AS ride_payment_method,
    COALESCE(r.carType, '') AS car_type,
    COALESCE(r.created_at, '1970-01-01 00:00:00') AS ride_created_at,
    COALESCE(r.updated_at, '1970-01-01 00:00:00') AS ride_updated_at,
    COALESCE(r.DriverIsGoingToPassenger, 0) AS driver_is_going_to_passenger,
    COALESCE(r.rideTimeStart, '1970-01-01 00:00:00') AS ride_time_start,
    COALESCE(r.rideTimeFinish, '1970-01-01 00:00:00') AS ride_time_finish,
    COALESCE(r.price_for_driver, 0) AS price_for_driver,
    COALESCE(r.price_for_passenger, 0) AS price_for_passenger,
    COALESCE(r.distance, 0) AS distance,
    COALESCE(pw.balance, 0) AS passenger_wallet_balance,
    COALESCE(pay.amount, 0) AS passenger_payment_amount,
    COALESCE(pay.payment_method, '') AS passenger_payment_method,
    COALESCE(dw.amount, 0) AS driver_payment_amount,
    COALESCE(dw.paymentMethod, '') AS driver_payment_method
FROM 
    passengers p
LEFT JOIN 
    ride r ON p.id = r.passenger_id
LEFT JOIN 
    passengerWallet pw ON p.id = pw.passenger_id
LEFT JOIN 
    payments pay ON r.id = pay.rideId
LEFT JOIN 
    driverWallet dw ON r.driver_id = dw.driverID AND pay.id = dw.paymentID
WHERE 
    p.phone = :phone
    AND r.id = (
        SELECT id 
        FROM ride 
        WHERE passenger_id = p.id 
        ORDER BY date DESC, time DESC 
        LIMIT 1
    )";

$stmt = $con->prepare($sql);
$stmt->bindParam(':phone', $phoneEncrypted);
$stmt->execute();

if ($stmt->rowCount() > 0) {
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // فك التشفير للحقول الحساسة
    foreach ($rows as &$row) {
        if (isset($row['phone']))         $row['phone'] = $encryptionHelper->decryptData($row['phone']);
        if (isset($row['email']))         $row['email'] = $encryptionHelper->decryptData($row['email']);
        if (isset($row['gender']))        $row['gender'] = $encryptionHelper->decryptData($row['gender']);
        if (isset($row['birthdate']))     $row['birthdate'] = $encryptionHelper->decryptData($row['birthdate']);
        if (isset($row['site']))          $row['site'] = $encryptionHelper->decryptData($row['site']);
        if (isset($row['first_name']))    $row['first_name'] = $encryptionHelper->decryptData($row['first_name']);
        if (isset($row['last_name']))     $row['last_name'] = $encryptionHelper->decryptData($row['last_name']);
        if (isset($row['employmentType']))$row['employmentType'] = $encryptionHelper->decryptData($row['employmentType']);
        if (isset($row['maritalStatus'])) $row['maritalStatus'] = $encryptionHelper->decryptData($row['maritalStatus']);
       unset($r['password']);
    }

    jsonSuccess($rows);
} else {
    jsonError("No wallet record found");
}
?>