<?php
// api/get-supervisors.php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, 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;
}

try {
    // استعلام مع الأعمدة الجديدة
    $query = "SELECT id, username, full_name, email, phone, role, governorate, area, is_active, created_at 
              FROM users 
              WHERE role IN ('supervisor', 'supervisor-governorate', 'supervisor-area')
              ORDER BY created_at DESC";
    
    $stmt = $pdo->query($query);
    $supervisors = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo json_encode([
        'success' => true,
        'data' => $supervisors,
        'count' => count($supervisors)
    ]);
    
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'message' => 'خطأ في تنفيذ الاستعلام: ' . $e->getMessage()]);
}
?>