<?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("فشل الاتصال بقاعدة البيانات");
    }
    
    $user_id = $_GET['user_id'] ?? 0;
    
    if (!$user_id) {
        throw new Exception("معرف المستخدم مطلوب");
    }
    
    // بيانات المستخدم
    $userQuery = "SELECT 
                    u.first_name,
                    u.last_name,
                    u.email,
                    u.phone,
                    u.created_at,
                    u.last_login as last_active,
                    s.city
                  FROM users u
                  LEFT JOIN supervisors s ON u.id = s.user_id
                  WHERE u.id = :user_id AND u.role = 'supervisor'";
    $userStmt = $db->prepare($userQuery);
    $userStmt->execute(['user_id' => $user_id]);
    $user = $userStmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$user) {
        throw new Exception("المستخدم غير موجود");
    }
    
    // إحصائيات المراجعات
    $statsQuery = "SELECT 
                    COUNT(*) as reviewed,
                    SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) as approved,
                    AVG(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) * 100 as accuracy
                   FROM inspection_requests
                   WHERE approved_by = :user_id";
    $statsStmt = $db->prepare($statsQuery);
    $statsStmt->execute(['user_id' => $user_id]);
    $stats = $statsStmt->fetch(PDO::FETCH_ASSOC);
    
    // آخر النشاطات
    $activitiesQuery = "SELECT 
                        al.action,
                        al.details as title,
                        al.created_at
                       FROM activity_logs al
                       WHERE al.user_id = :user_id
                       ORDER BY al.created_at DESC
                       LIMIT 10";
    $activitiesStmt = $db->prepare($activitiesQuery);
    $activitiesStmt->execute(['user_id' => $user_id]);
    $activities = $activitiesStmt->fetchAll(PDO::FETCH_ASSOC);
    
    // إضافة أيقونات للنشاطات
    foreach ($activities as &$activity) {
        $activity['icon'] = 'fa-check-circle';
        if (strpos($activity['action'], 'approve') !== false) {
            $activity['icon'] = 'fa-check';
        } elseif (strpos($activity['action'], 'reject') !== false) {
            $activity['icon'] = 'fa-times';
        } elseif (strpos($activity['action'], 'login') !== false) {
            $activity['icon'] = 'fa-sign-in-alt';
        }
    }
    
    $data = [
        'first_name' => $user['first_name'],
        'last_name' => $user['last_name'],
        'email' => $user['email'],
        'phone' => $user['phone'],
        'city' => $user['city'],
        'created_at' => $user['created_at'],
        'last_active' => $user['last_active'],
        'stats' => [
            'reviewed' => (int)$stats['reviewed'],
            'approved' => (int)$stats['approved'],
            'accuracy' => round((float)$stats['accuracy'], 1)
        ],
        'activities' => $activities
    ];
    
    echo json_encode([
        'success' => true,
        'data' => $data
    ]);
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage()
    ]);
}
?>