<?php
require_once '../../config/database.php';
require_once '../../models/user.php';

session_start();

$database = new Database();
$db = $database->getConnection();
$user_model = new User($db);

$all_users = $user_model->getAllUsers() ?? [];

$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_user'])) {
    $user_id = $_POST['user_id'];
    if ($user_model->deleteUser($user_id)) {
        $message = 'تم حذف المستخدم بنجاح!';
        $all_users = $user_model->getAllUsers() ?? [];
    } else {
        $message = 'حدث خطأ أثناء حذف المستخدم';
    }
}
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>إدارة المستخدمين - Roz Skin</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body { font-family: 'Tajawal', sans-serif; }
    </style>
</head>
<body class="bg-gray-50">
    <div class="min-h-screen">
        <!-- Header -->
        <header class="bg-white shadow-sm border-b border-gray-200 sticky top-0 z-20">
            <div class="flex items-center justify-between px-6 py-4">
                <div class="flex items-center space-x-4 space-x-reverse">
                    <a href="../index.php" class="text-gray-600 hover:text-gray-900">
                        <i class="fas fa-arrow-right"></i>
                    </a>
                    <h1 class="text-2xl font-bold text-gray-900">إدارة المستخدمين</h1>
                </div>
            </div>
        </header>

        <div class="p-6">
            <?php if ($message): ?>
                <div class="bg-green-50 border-r-4 border-green-400 p-4 mb-6">
                    <p class="text-sm text-green-700"><?php echo $message; ?></p>
                </div>
            <?php endif; ?>

            <!-- Statistics Cards -->
            <?php
                $stats = [
                    'total' => count($all_users),
                    'admins' => count(array_filter($all_users, fn($u) => $u['role'] === 'admin')),
                    'users' => count(array_filter($all_users, fn($u) => $u['role'] === 'user')),
                    'verified' => count(array_filter($all_users, fn($u) => !empty($u['is_verified'])))
                ];
            ?>
            <div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-6">
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-sm text-gray-600">إجمالي المستخدمين</p>
                            <p class="text-2xl font-bold text-gray-900"><?php echo $stats['total']; ?></p>
                        </div>
                        <div class="w-12 h-12 bg-blue-100 rounded-lg flex items-center justify-center">
                            <i class="fas fa-users text-blue-600 text-xl"></i>
                        </div>
                    </div>
                </div>
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-sm text-gray-600">المديرون</p>
                            <p class="text-2xl font-bold text-purple-600"><?php echo $stats['admins']; ?></p>
                        </div>
                        <div class="w-12 h-12 bg-purple-100 rounded-lg flex items-center justify-center">
                            <i class="fas fa-user-shield text-purple-600 text-xl"></i>
                        </div>
                    </div>
                </div>
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-sm text-gray-600">العملاء</p>
                            <p class="text-2xl font-bold text-green-600"><?php echo $stats['users']; ?></p>
                        </div>
                        <div class="w-12 h-12 bg-green-100 rounded-lg flex items-center justify-center">
                            <i class="fas fa-user text-green-600 text-xl"></i>
                        </div>
                    </div>
                </div>
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-sm text-gray-600">موثقون</p>
                            <p class="text-2xl font-bold text-blue-600"><?php echo $stats['verified']; ?></p>
                        </div>
                        <div class="w-12 h-12 bg-blue-100 rounded-lg flex items-center justify-center">
                            <i class="fas fa-check-circle text-blue-600 text-xl"></i>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Users Table -->
            <div class="bg-white rounded-lg shadow-sm border border-gray-200">
                <div class="px-6 py-5 border-b border-gray-200">
                    <h3 class="text-xl font-semibold text-gray-900">جميع المستخدمين (<?php echo count($all_users); ?>)</h3>
                </div>
                <div class="overflow-x-auto">
                    <?php if (!empty($all_users)): ?>
                        <table class="min-w-full divide-y divide-gray-200">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">الصورة</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">الاسم</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">الهاتف</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">العمر</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">المتابعون</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">الدور</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">تاريخ التسجيل</th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">الإجراءات</th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-gray-200">
                                <?php foreach ($all_users as $user): ?>
                                    <tr class="hover:bg-gray-50">
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            <?php if (!empty($user['profile_picture'])): ?>
                                                <img src="../../<?php echo htmlspecialchars($user['profile_picture']); ?>" 
                                                     alt="<?php echo htmlspecialchars($user['name']); ?>" 
                                                     class="w-10 h-10 rounded-full object-cover">
                                            <?php else: ?>
                                                <div class="w-10 h-10 bg-gradient-to-br from-purple-400 to-pink-400 rounded-full flex items-center justify-center">
                                                    <span class="text-white font-bold"><?php echo mb_substr($user['name'], 0, 1); ?></span>
                                                </div>
                                            <?php endif; ?>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            <div class="text-sm font-medium text-gray-900"><?php echo htmlspecialchars($user['name']); ?></div>
                                            <?php if (!empty($user['is_verified'])): ?>
                                                <span class="inline-flex items-center text-xs text-blue-600">
                                                    <i class="fas fa-check-circle ml-1"></i>موثق
                                                </span>
                                            <?php endif; ?>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                            <?php if (!empty($user['phone'])): ?>
                                                <a href="https://wa.me/2<?php echo ltrim($user['phone'], '0'); ?>" target="_blank" class="text-green-600 hover:text-green-700">
                                                    <i class="fab fa-whatsapp ml-1"></i><?php echo htmlspecialchars($user['phone']); ?>
                                                </a>
                                            <?php else: ?>
                                                <span class="text-gray-400">-</span>
                                            <?php endif; ?>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                            <?php echo !empty($user['age']) ? $user['age'] . ' سنة' : '-'; ?>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                            <div class="flex items-center gap-2">
                                                <span><i class="fas fa-users text-purple-600"></i> <?php echo $user['total_followers'] ?? 0; ?></span>
                                                <span class="text-gray-400">|</span>
                                                <span><i class="fas fa-heart text-red-600"></i> <?php echo $user['total_likes'] ?? 0; ?></span>
                                            </div>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium <?php echo $user['role'] === 'admin' ? 'bg-purple-100 text-purple-800' : 'bg-blue-100 text-blue-800'; ?>">
                                                <?php echo $user['role'] === 'admin' ? 'مدير' : 'عميل'; ?>
                                            </span>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?php echo date('Y-m-d', strtotime($user['created_at'])); ?></td>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm">
                                            <?php if ($user['role'] !== 'admin'): ?>
                                                <form method="POST" class="inline-block" onsubmit="return confirm('هل أنت متأكد من حذف هذا المستخدم؟');">
                                                    <input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
                                                    <button type="submit" name="delete_user" class="text-red-600 hover:text-red-900">
                                                        <i class="fas fa-trash"></i>
                                                    </button>
                                                </form>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    <?php else: ?>
                        <div class="p-8 text-center text-gray-500">
                            <i class="fas fa-users text-4xl mb-4"></i>
                            <p>لا يوجد مستخدمين حالياً</p>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
