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

session_start();

$database = new Database();
$db = $database->getConnection();
$order_model = new Order($db);
$product_model = new Product($db);
$user_model = new User($db);

$all_orders = $order_model->getAllOrders();
$total_sales = array_reduce($all_orders, function($total, $order) {
    return $total + $order['total_amount'];
}, 0);

$all_products_stmt = $product_model->getAll();
$all_products = $all_products_stmt ? $all_products_stmt->fetchAll(PDO::FETCH_ASSOC) : [];
$total_users = count($user_model->getAllUsers() ?? []);
?>
<!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="../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">
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
                <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-sm font-medium text-gray-600">إجمالي الطلبات</p>
                            <p class="text-3xl font-bold text-gray-900"><?php echo count($all_orders); ?></p>
                        </div>
                        <div class="bg-blue-50 p-3 rounded-lg">
                            <i class="fas fa-shopping-cart text-blue-600 text-2xl"></i>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-sm font-medium text-gray-600">إجمالي المبيعات</p>
                            <p class="text-3xl font-bold text-gray-900">EGP <?php echo number_format($total_sales, 0); ?></p>
                        </div>
                        <div class="bg-green-50 p-3 rounded-lg">
                            <i class="fas fa-dollar-sign text-green-600 text-2xl"></i>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-sm font-medium text-gray-600">إجمالي المنتجات</p>
                            <p class="text-3xl font-bold text-gray-900"><?php echo count($all_products); ?></p>
                        </div>
                        <div class="bg-yellow-50 p-3 rounded-lg">
                            <i class="fas fa-box text-yellow-600 text-2xl"></i>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-sm font-medium text-gray-600">إجمالي العملاء</p>
                            <p class="text-3xl font-bold text-gray-900"><?php echo $total_users; ?></p>
                        </div>
                        <div class="bg-purple-50 p-3 rounded-lg">
                            <i class="fas fa-users text-purple-600 text-2xl"></i>
                        </div>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
                <h3 class="text-xl font-semibold text-gray-900 mb-4">تقارير مفصلة</h3>
                <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                    <button class="p-4 border border-gray-200 rounded-lg hover:bg-gray-50 text-right">
                        <i class="fas fa-file-pdf text-red-600 text-2xl mb-2"></i>
                        <p class="font-semibold">تقرير المبيعات الشهري</p>
                    </button>
                    <button class="p-4 border border-gray-200 rounded-lg hover:bg-gray-50 text-right">
                        <i class="fas fa-chart-line text-blue-600 text-2xl mb-2"></i>
                        <p class="font-semibold">تحليل الأداء</p>
                    </button>
                    <button class="p-4 border border-gray-200 rounded-lg hover:bg-gray-50 text-right">
                        <i class="fas fa-users text-green-600 text-2xl mb-2"></i>
                        <p class="font-semibold">تقرير العملاء</p>
                    </button>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
