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

$driver_id = filterRequest("driver_id");
$phone     = filterRequest("phone");
$reason    = filterRequest("reason"); // يمكن أن يأتي من البارامتر أو نخليه افتراضي

if (empty($driver_id) || empty($phone)) {
    jsonError("Driver ID and phone are required.");
    exit;
}

try {
    // تشفير رقم الهاتف
    $encPhone = $encryptionHelper->encryptData($phone);

    // حذف السائق من جدول driver
    $sqlDel = "DELETE FROM driver WHERE id = :id";
    $stmtDel = $con->prepare($sqlDel);
    $stmtDel->bindParam(':id', $driver_id, PDO::PARAM_INT);
    $stmtDel->execute();

    if ($stmtDel->rowCount() > 0) {
        // إضافة بيانات السائق المحذوف إلى البلاك ليست
        $sqlInsert = "INSERT INTO blacklist_driver (driver_id, phone, reason) 
                      VALUES (:driver_id, :phone, :reason)";
        $stmtInsert = $con->prepare($sqlInsert);
        $stmtInsert->execute([
            'driver_id' => $driver_id,
            'phone'     => $encPhone,
            'reason'    => !empty($reason) ? $reason : "Deleted & blacklisted by admin"
        ]);

        jsonSuccess(null, "Driver deleted and blacklisted successfully.");
    } else {
        jsonError("No driver found with the provided ID.");
    }

} catch (PDOException $e) {
    jsonError("Error: " . $e->getMessage());
}