<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');

require_once __DIR__ . '/../../database.php';

try {
    $database = new Database();
    $db = $database->getConnection();
    
    if (!$db) {
        throw new Exception("فشل الاتصال بقاعدة البيانات");
    }
    
    $client_id = $_GET['client_id'] ?? 0;
    
    if (!$client_id) {
        throw new Exception("معرف العميل مطلوب");
    }
    
    $query = "SELECT 
                id,
                brand,
                model,
                year,
                plate_number,
                chassis_number,
                color,
                created_at
              FROM client_cars
              WHERE client_id = :client_id
              ORDER BY created_at DESC";
    
    $stmt = $db->prepare($query);
    $stmt->execute(['client_id' => $client_id]);
    
    $cars = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo json_encode([
        'success' => true,
        'data' => $cars
    ]);
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage()
    ]);
}
?>