<?php
session_start();

if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header('Location: ../login.php');
    exit;
}

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

try {
    $database = new Database();
    $db = $database->getConnection();
    
    // Get wishlist items
    $query = "SELECT w.*, u.name as user_name, u.phone,
              p.name as product_name, p.price, p.image, p.stock_quantity
              FROM wishlist w
              JOIN users u ON w.user_id = u.id
              JOIN products p ON w.product_id = p.id
              ORDER BY w.created_at DESC";
    $stmt = $db->prepare($query);
    $stmt->execute();
    $wishlist_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    // Get most wanted products
    $query = "SELECT p.id, p.name, p.price, p.image, p.stock_quantity,
              COUNT(w.id) as wishlist_count
              FROM products p
              JOIN wishlist w ON p.id = w.product_id
              GROUP BY p.id
              ORDER BY wishlist_count DESC
              LIMIT 10";
    $stmt = $db->prepare($query);
    $stmt->execute();
    $top_products = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
} catch (Exception $e) {
    $error = "خطأ: " . $e->getMessage();
    $wishlist_items = [];
    $top_products = [];
}
?>
<!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 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="../dashboard.php" class="text-gray-600 hover:text-gray-900">
                        <i class="fas fa-arrow-right text-xl"></i>
                    </a>
                    <h1 class="text-2xl font-bold text-gray-900">قائمة الأمنيات</h1>
                </div>
            </div>
        </header>

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

            <!-- Statistics Cards -->
            <?php
                $total_items = count($wishlist_items);
                $unique_users = count(array_unique(array_column($wishlist_items, 'user_id')));
                $unique_products = count(array_unique(array_column($wishlist_items, 'product_id')));
            ?>
            <div class="grid grid-cols-1 md:grid-cols-3 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 $total_items; ?></p>
                        </div>
                        <div class="w-12 h-12 bg-pink-100 rounded-lg flex items-center justify-center">
                            <i class="fas fa-heart text-pink-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 $unique_users; ?></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 $unique_products; ?></p>
                        </div>
                        <div class="w-12 h-12 bg-purple-100 rounded-lg flex items-center justify-center">
                            <i class="fas fa-star text-purple-600 text-xl"></i>
                        </div>
                    </div>
                </div>
            </div>

            <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
                <!-- Top Products -->
                <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">المنتجات الأكثر طلباً</h3>
                    </div>
                    <div class="p-6">
                        <?php if (!empty($top_products)): ?>
                            <div class="space-y-3">
                                <?php foreach ($top_products as $product): ?>
                                    <div class="flex items-center gap-4 p-3 bg-gray-50 rounded-lg">
                                        <?php if (!empty($product['image'])): ?>
                                            <img src="../../<?php echo htmlspecialchars($product['image']); ?>" 
                                                 alt="<?php echo htmlspecialchars($product['name']); ?>" 
                                                 class="w-16 h-16 object-cover rounded">
                                        <?php else: ?>
                                            <div class="w-16 h-16 bg-gray-200 rounded flex items-center justify-center">
                                                <i class="fas fa-box text-gray-400"></i>
                                            </div>
                                        <?php endif; ?>
                                        <div class="flex-1">
                                            <h4 class="font-semibold text-gray-900"><?php echo htmlspecialchars($product['name']); ?></h4>
                                            <p class="text-sm text-gray-600">EGP <?php echo number_format($product['price'], 0); ?></p>
                                        </div>
                                        <div class="text-center">
                                            <div class="flex items-center gap-1 text-pink-600">
                                                <i class="fas fa-heart"></i>
                                                <span class="font-bold"><?php echo $product['wishlist_count']; ?></span>
                                            </div>
                                            <p class="text-xs text-gray-500 mt-1">مخزون: <?php echo $product['stock_quantity']; ?></p>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        <?php else: ?>
                            <p class="text-center text-gray-500 py-8">لا توجد منتجات في قوائم الأمنيات</p>
                        <?php endif; ?>
                    </div>
                </div>

                <!-- Recent Wishlist Items -->
                <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">آخر الإضافات</h3>
                    </div>
                    <div class="p-6">
                        <?php if (!empty($wishlist_items)): ?>
                            <div class="space-y-3">
                                <?php foreach (array_slice($wishlist_items, 0, 10) as $item): ?>
                                    <div class="flex items-center gap-4 p-3 bg-gray-50 rounded-lg">
                                        <?php if (!empty($item['image'])): ?>
                                            <img src="../../<?php echo htmlspecialchars($item['image']); ?>" 
                                                 alt="<?php echo htmlspecialchars($item['product_name']); ?>" 
                                                 class="w-12 h-12 object-cover rounded">
                                        <?php else: ?>
                                            <div class="w-12 h-12 bg-gray-200 rounded flex items-center justify-center">
                                                <i class="fas fa-box text-gray-400 text-sm"></i>
                                            </div>
                                        <?php endif; ?>
                                        <div class="flex-1">
                                            <h4 class="font-semibold text-sm text-gray-900"><?php echo htmlspecialchars($item['product_name']); ?></h4>
                                            <p class="text-xs text-gray-600"><?php echo htmlspecialchars($item['user_name']); ?></p>
                                        </div>
                                        <div class="text-left">
                                            <p class="text-sm font-bold text-gray-900">EGP <?php echo number_format($item['price'], 0); ?></p>
                                            <p class="text-xs text-gray-500"><?php echo date('Y-m-d', strtotime($item['created_at'])); ?></p>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        <?php else: ?>
                            <p class="text-center text-gray-500 py-8">لا توجد عناصر في قوائم الأمنيات</p>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
