<?php
// api/update-inspector-permissions.php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit;
}

// بيانات الاتصال بقاعدة البيانات
$host = 'localhost';
$dbname = 'digitals_autotester_db';
$username = 'digitals';
$password = 'bassam.bassam';

try {
    $pdo = new PDO(
        "mysql:host=$host;dbname=$dbname;charset=utf8mb4",
        $username,
        $password
    );
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'message' => 'خطأ في الاتصال بقاعدة البيانات']);
    exit;
}

// استقبال البيانات
$input = file_get_contents('php://input');
$data = json_decode($input, true);

if (!$data) {
    echo json_encode(['success' => false, 'message' => 'بيانات غير صالحة']);
    exit;
}

// التحقق من البيانات المطلوبة
if (empty($data['inspectorId'])) {
    echo json_encode(['success' => false, 'message' => 'معرف الفاحص مطلوب']);
    exit;
}

try {
    $updateFields = [];
    $params = [];
    
    // تحديث الحقول المرسلة فقط
    if (isset($data['specialization'])) {
        $updateFields[] = "specialization = ?";
        $params[] = $data['specialization'];
    }
    
    if (isset($data['grade'])) {
        $updateFields[] = "grade = ?";
        $params[] = $data['grade'];
    }
    
    if (isset($data['governorate'])) {
        $updateFields[] = "governorate = ?";
        $params[] = $data['governorate'];
    }
    
    if (isset($data['area'])) {
        $updateFields[] = "area = ?";
        $params[] = $data['area'];
    }
    
    if (isset($data['max_daily_requests'])) {
        $updateFields[] = "max_daily_requests = ?";
        $params[] = $data['max_daily_requests'];
    }
    
    if (isset($data['permissions'])) {
        $updateFields[] = "permissions = ?";
        $params[] = json_encode($data['permissions']);
    }
    
    if (isset($data['available'])) {
        $updateFields[] = "available = ?";
        $params[] = $data['available'] ? 1 : 0;
    }
    
    if (isset($data['years_experience'])) {
        $updateFields[] = "years_experience = ?";
        $params[] = $data['years_experience'];
    }
    
    if (isset($data['qualification'])) {
        $updateFields[] = "qualification = ?";
        $params[] = $data['qualification'];
    }
    
    if (isset($data['skills'])) {
        $updateFields[] = "skills = ?";
        $params[] = $data['skills'];
    }
    
    if (empty($updateFields)) {
        echo json_encode(['success' => false, 'message' => 'لا توجد بيانات للتحديث']);
        exit;
    }
    
    $params[] = $data['inspectorId'];
    $query = "UPDATE inspectors SET " . implode(", ", $updateFields) . " WHERE user_id = ?";
    
    $stmt = $pdo->prepare($query);
    $stmt->execute($params);
    
    echo json_encode([
        'success' => true,
        'message' => 'تم تحديث بيانات الفاحص بنجاح'
    ]);
    
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'message' => 'خطأ في قاعدة البيانات: ' . $e->getMessage()]);
}
?>