<?php

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

$sql = "SELECT 
    phone_verification.*,
    notesForDriverService.note
FROM 
    phone_verification
INNER JOIN -- نستخدم INNER JOIN لضمان جلب من لديهم ملاحظات فقط
    `notesForDriverService` 
ON 
    `notesForDriverService`.`phone` = `phone_verification`.`phone_number`
WHERE 
    `notesForDriverService`.`note` != 'delete'
ORDER BY 
    `phone_verification`.`created_at` DESC -- الترتيب حسب تاريخ التحقق لأنه العمود الموجود
LIMIT 400;
";

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

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

    // فك تشفير أرقام الهواتف فقط للإخراج
   foreach ($rows as &$row) {
        if (!empty($row['phone'])) {
            $row['phone'] = $encryptionHelper->decryptData($row['phone']);
        }
        if (!empty($row['email'])) {
            $row['email'] = $encryptionHelper->decryptData($row['email']);
        }
       if (isset($row['phone_number'])) {
        $row['phone_number'] = $encryptionHelper->decryptData($row['phone_number']);
    }
    }
 

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