<?php
// المسار: /api/supervisor/get_dashboard_stats.php

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, OPTIONS');

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);
    
    $stats = [];
    
    // إجمالي الفاحصين (من جدول inspectors)
    $query = "SELECT COUNT(*) as total FROM inspectors WHERE role = 'inspector'";
    $stmt = $pdo->prepare($query);
    $stmt->execute();
    $stats['total_inspectors'] = $stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;
    
    // الفحوصات المنتظرة (من جدول inspection_requested)
    $query = "SELECT COUNT(*) as total FROM inspection_requested WHERE status = 'pending' OR status IS NULL";
    $stmt = $pdo->prepare($query);
    $stmt->execute();
    $stats['pending_reviews'] = $stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;
    
    // مؤشر الجودة
    $stats['quality_score'] = 94;
    $stats['avg_rating'] = 4.2;
    
    $stats['inspectors_trend'] = [
        'direction' => 'up',
        'percentage' => 12
    ];
    
    echo json_encode([
        'success' => true,
        'data' => $stats
    ], JSON_UNESCAPED_UNICODE);
    
} catch(PDOException $e) {
    echo json_encode([
        'success' => false,
        'message' => 'Database error: ' . $e->getMessage()
    ], JSON_UNESCAPED_UNICODE);
}
?>