<?php
include 'auth.php';
include_once '../config/database.php';

// Fetch Statistics
$total_users = $conn->query("SELECT COUNT(*) FROM users")->fetchColumn();
$total_games = $conn->query("SELECT COUNT(*) FROM games")->fetchColumn();
$total_bookings = $conn->query("SELECT COUNT(*) FROM bookings")->fetchColumn();
$pending_bookings = $conn->query("SELECT COUNT(*) FROM bookings WHERE status = 'Pending'")->fetchColumn();

ob_start();
?>

<div class="row g-4 mb-4">
    <div class="col-md-3">
        <div class="card p-3 text-center">
            <div class="icon-box bg-primary bg-opacity-10 p-3 rounded-circle mx-auto mb-3" style="width: 60px;">
                <i class="fas fa-users text-primary fs-3"></i>
            </div>
            <h3 class="fw-bold"><?= $total_users ?></h3>
            <p class="text-muted mb-0">المستخدمين</p>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card p-3 text-center">
            <div class="icon-box bg-success bg-opacity-10 p-3 rounded-circle mx-auto mb-3" style="width: 60px;">
                <i class="fas fa-gamepad text-success fs-3"></i>
            </div>
            <h3 class="fw-bold"><?= $total_games ?></h3>
            <p class="text-muted mb-0">الألعاب</p>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card p-3 text-center">
            <div class="icon-box bg-info bg-opacity-10 p-3 rounded-circle mx-auto mb-3" style="width: 60px;">
                <i class="fas fa-calendar-check text-info fs-3"></i>
            </div>
            <h3 class="fw-bold"><?= $total_bookings ?></h3>
            <p class="text-muted mb-0">إجمالي الحجوزات</p>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card p-3 text-center">
            <div class="icon-box bg-warning bg-opacity-10 p-3 rounded-circle mx-auto mb-3" style="width: 60px;">
                <i class="fas fa-clock text-warning fs-3"></i>
            </div>
            <h3 class="fw-bold"><?= $pending_bookings ?></h3>
            <p class="text-muted mb-0">حجوزات معلقة</p>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-8">
        <div class="card p-4">
            <h5 class="fw-bold mb-4">آخر الحجوزات</h5>
            <table class="table align-middle">
                <thead>
                    <tr>
                        <th>العميل</th>
                        <th>اللعبة</th>
                        <th>الحالة</th>
                        <th>التاريخ</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $latest_bookings = $conn->query("SELECT b.*, u.name as user_name, g.name as game_name 
                                                   FROM bookings b 
                                                   JOIN users u ON b.user_id = u.id 
                                                   JOIN games g ON b.game_id = g.id 
                                                   ORDER BY b.id DESC LIMIT 5")->fetchAll();
                    foreach($latest_bookings as $booking):
                    ?>
                    <tr>
                        <td><?= htmlspecialchars($booking['user_name']) ?></td>
                        <td><?= htmlspecialchars($booking['game_name']) ?></td>
                        <td>
                            <span class="badge badge-<?= strtolower($booking['status']) ?>">
                                <?= $booking['status'] ?>
                            </span>
                        </td>
                        <td><?= date('Y-m-d', strtotime($booking['created_at'])) ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card p-4">
            <h5 class="fw-bold mb-4">أهم الألعاب</h5>
            <?php
            $popular_games = $conn->query("SELECT g.name, COUNT(b.id) as count 
                                         FROM games g 
                                         LEFT JOIN bookings b ON g.id = b.game_id 
                                         GROUP BY g.id ORDER BY count DESC LIMIT 5")->fetchAll();
            foreach($popular_games as $game):
            ?>
            <div class="d-flex justify-content-between align-items-center mb-3">
                <span><?= htmlspecialchars($game['name']) ?></span>
                <span class="badge bg-primary rounded-pill"><?= $game['count'] ?> حجز</span>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>

<?php
$content = ob_get_clean();
$page_title = 'الإحصائيات العامة';
include 'layout.php';
?>
