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

$phone = filterRequest("phone");
$encryptedPhone = $encryptionHelper->encryptData($phone); // تشفير الهاتف

$sql = "SELECT
    COALESCE(
        (
        SELECT COUNT(*) FROM `ride` WHERE `ride`.`driver_id` = d.id
        ),
    0) AS countRide,
    
    COALESCE(
        (
        SELECT AVG(`ratingDriver`.`rating`)
        FROM ratingDriver
        WHERE `ratingDriver`.`driver_id` = d.id
        ),
    0) AS rating,
    
    COALESCE(
        (
        SELECT SUM(pd.amount)
        FROM `payments` pd
        WHERE pd.driverID = d.id
        ),
    0) AS totalPayment,

    COALESCE(
        (
        SELECT SUM(dw.amount)
        FROM `driverWallet` dw
        WHERE dw.driverID = d.id
        ),
    0) AS totalDriverWallet,

    COALESCE(
        (
        SELECT COUNT(*)
        FROM complaint
        WHERE complaint.driver_id = d.id
        ),
    0) AS countComplaint,

    COALESCE(
        (
        SELECT COUNT(*)
        FROM driver_ride_scam scam
        WHERE scam.driverID = d.id
        ),
    0) AS countScam,

    COALESCE(
        (
        SELECT complaint.description
        FROM complaint
        WHERE complaint.driver_id = d.id
        ORDER BY complaint.date_resolved DESC
        LIMIT 1
        ),
    ''
    ) AS complaint,

    COALESCE(
        (
        SELECT COUNT(*)
        FROM ratingPassenger
        WHERE ratingPassenger.driverID = d.id
        ),
    0) AS DRatingPassengersCount,

    COALESCE(
        (
        SELECT AVG(ratingPassenger.rating)
        FROM ratingPassenger
        WHERE ratingPassenger.driverID = d.id
        ),
    0) AS avgDRatingPassenger,

    cr.*,
    d.*
FROM driver d
LEFT JOIN CarRegistration cr ON cr.driverID = d.id
WHERE d.phone = :phone;
";

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

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

    // فك تشفير الحقول المهمة
    foreach ($row as &$r) {
        if (isset($r['phone'])) $r['phone'] = $encryptionHelper->decryptData($r['phone']);
        if (isset($r['email'])) $r['email'] = $encryptionHelper->decryptData($r['email']);
        if (isset($r['first_name'])) $r['first_name'] = $encryptionHelper->decryptData($r['first_name']);
        if (isset($r['last_name'])) $r['last_name'] = $encryptionHelper->decryptData($r['last_name']);
        if (isset($r['gender'])) $r['gender'] = $encryptionHelper->decryptData($r['gender']);
        if (isset($r['birthdate'])) $r['birthdate'] = $encryptionHelper->decryptData($r['birthdate']);
        if (isset($r['site'])) $r['site'] = $encryptionHelper->decryptData($r['site']);
        if (isset($r['name_arabic'])) $r['name_arabic'] = $encryptionHelper->decryptData($r['name_arabic']);
      if (isset($r['national_number'])) $r['national_number'] = $encryptionHelper->decryptData($r['national_number']);
        if (isset($r['maritalStatus'])) $r['maritalStatus'] = $encryptionHelper->decryptData($r['maritalStatus']);
        if (isset($r['sosPhone'])) $r['sosPhone'] = $encryptionHelper->decryptData($r['sosPhone']);
        if (isset($r['car_plate'])) $r['car_plate'] = $encryptionHelper->decryptData($r['car_plate']);
        if (isset($r['owner'])) $r['owner'] = $encryptionHelper->decryptData($r['owner']);
        if (isset($r['address'])) $r['address'] = $encryptionHelper->decryptData($r['address']);
        if (isset($r['vin'])) $r['vin'] = $encryptionHelper->decryptData($r['vin']);
       unset($r['password']);
    }

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