<?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("فشل الاتصال بقاعدة البيانات");
    }
    
    $request_id = $_GET['id'] ?? 0;
    
    if (!$request_id) {
        throw new Exception("معرف الطلب مطلوب");
    }
    
    $query = "SELECT 
                ir.id,
                ir.request_number,
                ir.status,
                ir.created_at,
                ir.scheduled_date,
                ir.total_price as total,
                ir.price,
                ir.discount_amount as discount,
                ir.notes,
                CONCAT(u.first_name, ' ', u.last_name) as client_name,
                u.phone as client_phone,
                u.email as client_email,
                cc.brand as car_brand,
                cc.model as car_model,
                cc.year as car_year,
                cc.plate_number,
                cc.color as car_color,
                cc.chassis_number,
                it.name as inspection_type,
                ic.name as center_name,
                ic.address as center_address,
                ic.phone as center_phone,
                CONCAT(iu.first_name, ' ', iu.last_name) as inspector_name,
                p.payment_status,
                p.amount as payment_amount,
                p.payment_date,
                p.payment_method
              FROM inspection_requests ir
              JOIN clients c ON ir.client_id = c.id
              JOIN users u ON c.user_id = u.id
              JOIN client_cars cc ON ir.car_id = cc.id
              JOIN inspection_types it ON ir.inspection_type_id = it.id
              LEFT JOIN inspection_centers ic ON ir.center_id = ic.id
              LEFT JOIN inspectors i ON ir.inspector_id = i.id
              LEFT JOIN users iu ON i.user_id = iu.id
              LEFT JOIN payments p ON ir.id = p.request_id
              WHERE ir.id = :request_id";
    
    $stmt = $db->prepare($query);
    $stmt->execute(['request_id' => $request_id]);
    
    $request = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$request) {
        throw new Exception("الطلب غير موجود");
    }
    
    // جلب نقاط التفتيش
    $checkpointsQuery = "SELECT 
                            checkpoint_name,
                            status,
                            notes,
                            image_urls
                         FROM inspection_checkpoints
                         WHERE request_id = :request_id";
    $checkpointsStmt = $db->prepare($checkpointsQuery);
    $checkpointsStmt->execute(['request_id' => $request_id]);
    $checkpoints = $checkpointsStmt->fetchAll(PDO::FETCH_ASSOC);
    
    // جلب الجدول الزمني للطلب
    $timelineQuery = "SELECT 
                        status,
                        notes,
                        created_at
                     FROM request_timeline
                     WHERE request_id = :request_id
                     ORDER BY created_at DESC";
    $timelineStmt = $db->prepare($timelineQuery);
    $timelineStmt->execute(['request_id' => $request_id]);
    $timeline = $timelineStmt->fetchAll(PDO::FETCH_ASSOC);
    
    $request['checkpoints'] = $checkpoints;
    $request['timeline'] = $timeline;
    
    echo json_encode([
        'success' => true,
        'data' => $request
    ]);
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage()
    ]);
}
?>