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

// استعلام رئيسي مع ربط صحيح وتشفير رقم الهاتف
$sql = "SELECT
    phone_verification_passenger.*,
    notesForPassengerService.note,
    notesForPassengerService.editor,
    notesForPassengerService.createdAt AS note_created_at
FROM
    phone_verification_passenger
LEFT JOIN notesForPassengerService 
    ON notesForPassengerService.phone = phone_verification_passenger.phone_number
WHERE
    phone_verification_passenger.phone_number NOT IN (
        SELECT phone FROM passengers WHERE phone IS NOT NULL
    )
    AND phone_verification_passenger.created_at >= DATE_SUB(CURDATE(), INTERVAL 4 DAY)
ORDER BY
    phone_verification_passenger.created_at DESC
LIMIT 25;
";

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

$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

// فك التشفير إذا كان مطلوباً (مثلاً إذا phone_number مشفّر)
foreach ($rows as &$row) {
    if (isset($row['phone_number'])) {
        $row['phone_number'] = $encryptionHelper->decryptData($row['phone_number']);
    }
    if (isset($row['note'])) {
        $row['note'] = $encryptionHelper->decryptData($row['note']); // إذا كانت مضافة مشفّرة
    }
}

if ($rows) {
    jsonSuccess($rows);
} else {
    jsonError("No phone verified passengers found");
}
?>