<?php
/**
 * جلب إحصائيات لوحة التحكم للمدير
 * api/admin/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();
}

require_once '../config/database.php';
require_once 'includes/auth.php';

validateAdminToken();

try {
    $database = new Database();
    $db = $database->getConnection();
    
    // إحصائيات المستخدمين
    $stats = [];
    
    // 1. permission_requests - طلبات الصلاحيات
    $stmt = $db->query("SELECT COUNT(*) as total, SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending FROM permission_requests");
    $stats['permission_requests'] = $stmt->fetch(PDO::FETCH_ASSOC);
    
    // 2. registration_requests - طلبات التسجيل
    $stmt = $db->query("SELECT COUNT(*) as total, SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending FROM registration_requests");
    $stats['registration_requests'] = $stmt->fetch(PDO::FETCH_ASSOC);
    
    // 3. supervisors_requests - طلبات المشرفين
    $stmt = $db->query("SELECT COUNT(*) as total, SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending FROM supervisors_requests");
    $stats['supervisors_requests'] = $stmt->fetch(PDO::FETCH_ASSOC);
    
    // 4. trainees_requests - طلبات المتدربين
    $stmt = $db->query("SELECT COUNT(*) as total, SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending FROM trainees_requests");
    $stats['trainees_requests'] = $stmt->fetch(PDO::FETCH_ASSOC);
    
    // 5. job_applications - طلبات التوظيف
    $stmt = $db->query("SELECT COUNT(*) as total, SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending FROM job_applications");
    $stats['job_applications'] = $stmt->fetch(PDO::FETCH_ASSOC);
    
    // 6. inspectors_requests - طلبات الفاحصين
    $stmt = $db->query("SELECT COUNT(*) as total, SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending FROM inspectors_requests");
    $stats['inspectors_requests'] = $stmt->fetch(PDO::FETCH_ASSOC);
    
    // 7. companies_requests - طلبات الشركات
    $stmt = $db->query("SELECT COUNT(*) as total, SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending FROM companies_requests");
    $stats['companies_requests'] = $stmt->fetch(PDO::FETCH_ASSOC);
    
    // 8. clients_requests - طلبات العملاء
    $stmt = $db->query("SELECT COUNT(*) as total, SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending FROM clients_requests");
    $stats['clients_requests'] = $stmt->fetch(PDO::FETCH_ASSOC);
    
    // 9. administrative_requests - الطلبات الإدارية
    $stmt = $db->query("SELECT COUNT(*) as total, SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending FROM administrative_requests");
    $stats['administrative_requests'] = $stmt->fetch(PDO::FETCH_ASSOC);
    
    // 10. admin_notifications - إشعارات المدير
    $stmt = $db->query("SELECT COUNT(*) as total, SUM(CASE WHEN is_read = 0 THEN 1 ELSE 0 END) as unread FROM admin_notifications");
    $stats['admin_notifications'] = $stmt->fetch(PDO::FETCH_ASSOC);
    
    // إجمالي الطلبات المعلقة
    $totalPending = 0;
    foreach ($stats as $key => $value) {
        if (isset($value['pending'])) {
            $totalPending += (int)$value['pending'];
        }
    }
    $stats['total_pending'] = $totalPending;
    
    // الفحوصات
    $stmt = $db->query("SELECT COUNT(*) as total FROM inspection_requests");
    $stats['inspections'] = ['total' => $stmt->fetch(PDO::FETCH_ASSOC)['total']];
    
    // المستخدمين النشطين
    $stmt = $db->query("SELECT COUNT(*) as total FROM users WHERE is_active = 1");
    $stats['active_users'] = ['total' => $stmt->fetch(PDO::FETCH_ASSOC)['total']];
    
    echo json_encode([
        'success' => true,
        'stats' => $stats,
        'timestamp' => date('Y-m-d H:i:s')
    ]);
    
} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['error' => 'خطأ في قاعدة البيانات: ' . $e->getMessage()]);
}
?>