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

$driverId = filterRequest("id");

if (empty($driverId)) {
    jsonError("driver_id is required.");
    exit;
}

try {
    // تفاصيل السائق
    $sql = "SELECT * FROM driver WHERE id = :id LIMIT 1";
    $stmt = $con->prepare($sql);
    $stmt->execute([':id' => $driverId]);
    $driver = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$driver) {
        jsonError("Driver not found.");
        exit;
    }

    // فك التشفير للحقول الحساسة
    foreach ($driver as $k => $v) {
        if (in_array($k, ['phone',
            'email',
            'first_name',
            'last_name',
            'national_number',
            'address','gender','site',
            'birthdate',
            'name_arabic'])) {
            $driver[$k] = $encryptionHelper->decryptData($v);
        }
    }

    // الوثائق
    $sql2 = "SELECT doc_type, image_name, link FROM driver_documents WHERE driverID = :id";
    $stmt2 = $con->prepare($sql2);
    $stmt2->execute([':id' => $driverId]);
    $docs = $stmt2->fetchAll(PDO::FETCH_ASSOC);

    printSuccess([
        "driver" => $driver,
        "documents" => $docs
    ]);
} catch (PDOException $e) {
    jsonError("Error: " . $e->getMessage());
}