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

// جلب البيانات
$sql = "SELECT
    COUNT(DISTINCT driverID) AS driver_count,
    `payments`.driverID,
    COALESCE(SUM(amount), 0) AS total_amount,
    `driver`.`phone`,
    `driver`.`name_arabic`,
    `driver`.`accountBank`,
    `driver`.`bankCode`,
    `driver`.`email`
FROM
    payments
LEFT JOIN `driver` ON `driver`.`id` = payments.driverID
WHERE
    isGiven = 'waiting' AND payment_method IN(
        'visa-in', 'visa', 'visaRide', 'TransferFrom',
        'payout', 'TransferTo', 'payFromSeferToDriver'
    )
    AND WEEK(`payments`.created_at) = WEEK(CURRENT_DATE)
GROUP BY
    driverID
HAVING
    COALESCE(SUM(amount), 0) > 0 AND total_amount > 100
LIMIT 0, 25";

$stmt = $con->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

// فك التشفير للحقول المطلوبة
foreach ($result as &$row) {
    $fieldsToDecrypt = ['phone', 'email', 'accountBank', 'bankCode', 'name_arabic'];
    foreach ($fieldsToDecrypt as $field) {
        if (isset($row[$field]) && $row[$field] !== null) {
            $row[$field] = $encryptionHelper->decryptData($row[$field]);
        }
    }
}

if ($stmt->rowCount() > 0) {
    jsonSuccess($result);
} else {
    jsonError("No wallet record found");
}
?>