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

$phoneNumber = filterRequest("phone_number");

$sql = "SELECT
    CR.`id`,
    CR.`driverID`,
    CR.`vin`,
    CR.`car_plate`,
    CR.`make`,
    CR.`model`,
    CR.`year`,
    CR.`expiration_date`,
    CR.`color`,
    CR.`owner`,
    CR.`color_hex`,
    CR.`address`,
    CR.`displacement`,
    CR.`fuel`,
    CR.`registration_date`,
    CR.`created_at`,
    d.phone
FROM
    `CarRegistration` CR
LEFT JOIN driver d ON d.id = CR.driverID
WHERE
    CR.`driverID` NOT IN (
        SELECT DISTINCT CPE.`driverId`
        FROM `carPlateEdit` CPE
    )
ORDER BY RAND()
LIMIT 10;
";

$stmt = $con->prepare($sql);
$stmt->execute();

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

    foreach ($rows as &$row) {
        $row['vin'] = $encryptionHelper->decryptData($row['vin']);
        $row['car_plate'] = $encryptionHelper->decryptData($row['car_plate']);
        $row['owner'] = $encryptionHelper->decryptData($row['owner']);
        $row['address'] = $encryptionHelper->decryptData($row['address']);
        $row['phone'] = $encryptionHelper->decryptData($row['phone']);
    }

    jsonSuccess($rows);
} else {
    jsonError($message = "No Car verified yet found");
}
?>
