<?php
session_start();
include_once("../config/db.php");

// Security: Ensure user is an admin before performing any action.
if(!isset($_SESSION['admin'])){
    header("Location: login.php");
    exit();
}

include 'admin_header.php';

// Fetch all dashboard stats in a single, more efficient query
$query = "
    SELECT
      (SELECT COUNT(*) FROM contact_messages WHERE email NOT LIKE 'Interested in:%' OR email IS NULL) as booking_count,
      (SELECT COUNT(*) FROM contact_messages WHERE email LIKE 'Interested in:%') as message_count,
      (SELECT COUNT(*) FROM gallery) as gallery_count,
      (SELECT COUNT(*) FROM services) as service_count,
      (SELECT COUNT(*) FROM blog) as blog_count,
      (SELECT COUNT(*) FROM reviews) as review_count
";

$result = mysqli_query($conn, $query);
$stats = $result ? mysqli_fetch_assoc($result) : [
    'message_count' => 0,
    'booking_count' => 0,
    'gallery_count' => 0,
    'service_count' => 0,
    'blog_count' => 0,
    'review_count' => 0,
];

?>

<div class="dashboard-hero">
    <h1>Dashboard Overview</h1>
    <p>Welcome back, <?php echo htmlspecialchars($_SESSION['admin'], ENT_QUOTES, 'UTF-8'); ?>! Here's what's happening today.</p>
</div>

<!-- Stats Grid -->
<div class="stats-grid">
    <div class="stat-card card-blog"> <!-- Using card-blog color for variety -->
        <div class="stat-content">
            <div class="stat-number"><?php echo $stats['booking_count']; ?></div>
            <div class="stat-label">Contact Messages</div>
        </div>
        <i class="fas fa-envelope-open-text stat-icon-bg"></i>
    </div>

    <div class="stat-card card-messages">
        <div class="stat-content">
            <div class="stat-number"><?php echo $stats['message_count']; ?></div>
            <div class="stat-label">Bookings</div>
        </div>
        <i class="fas fa-calendar-check stat-icon-bg"></i>
    </div>

    <div class="stat-card card-gallery">
        <div class="stat-content">
            <div class="stat-number"><?php echo $stats['gallery_count']; ?></div>
            <div class="stat-label">Gallery Images</div>
        </div>
        <i class="fas fa-images stat-icon-bg"></i>
    </div>

    <div class="stat-card card-services">
        <div class="stat-content">
            <div class="stat-number"><?php echo $stats['service_count']; ?></div>
            <div class="stat-label">Services</div>
        </div>
        <i class="fas fa-concierge-bell stat-icon-bg"></i>
    </div>

    <div class="stat-card card-blog">
        <div class="stat-content">
            <div class="stat-number"><?php echo $stats['blog_count']; ?></div>
            <div class="stat-label">Blog Posts</div>
        </div>
        <i class="fas fa-feather-alt stat-icon-bg"></i>
    </div>

    <div class="stat-card card-gallery"> <!-- Using gallery color for variety -->
        <div class="stat-content">
            <div class="stat-number"><?php echo $stats['review_count']; ?></div>
            <div class="stat-label">Reviews</div>
        </div>
        <i class="fas fa-star stat-icon-bg"></i>
    </div>
</div>
<?php include 'admin_footer.php'; ?>