<?php
require_once '../config.php';

// Ensure session is started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Check if user is logged in
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: ' . BASE_URL . '/admin/login');
    exit;
}

$conn = get_db_connection();

// Get stats
$stats = [
    'services' => $conn->query("SELECT COUNT(*) as count FROM services")->fetch_assoc()['count'] ?? 0,
    'reviews' => $conn->query("SELECT COUNT(*) as count FROM reviews")->fetch_assoc()['count'] ?? 0,
    'faqs' => $conn->query("SELECT COUNT(*) as count FROM faqs")->fetch_assoc()['count'] ?? 0,
    'appointments' => 0
];

// Check if appointments table exists before querying
$table_check = $conn->query("SHOW TABLES LIKE 'appointments'");
if ($table_check->num_rows > 0) {
    $stats['appointments'] = $conn->query("SELECT COUNT(*) as count FROM appointments")->fetch_assoc()['count'] ?? 0;
}

$page_title = 'Dashboard';
require_once 'includes/admin-header.php';
?>

<div class="stats-grid">
    <div class="stat-card">
        <div class="stat-icon blue">
            <i class="fas fa-tools"></i>
        </div>
        <div class="stat-data">
            <h3>Total Services</h3>
            <div class="value">
                <?php echo $stats['services']; ?>
            </div>
        </div>
    </div>

    <div class="stat-card">
        <div class="stat-icon green">
            <i class="fas fa-star"></i>
        </div>
        <div class="stat-data">
            <h3>Customer Reviews</h3>
            <div class="value">
                <?php echo $stats['reviews']; ?>
            </div>
        </div>
    </div>

    <div class="stat-card">
        <div class="stat-icon purple">
            <i class="fas fa-calendar-check"></i>
        </div>
        <div class="stat-data">
            <h3>Appointments</h3>
            <div class="value">
                <?php echo $stats['appointments']; ?>
            </div>
        </div>
    </div>

    <div class="stat-card">
        <div class="stat-icon orange">
            <i class="fas fa-question-circle"></i>
        </div>
        <div class="stat-data">
            <h3>FAQs</h3>
            <div class="value">
                <?php echo $stats['faqs']; ?>
            </div>
        </div>
    </div>
</div>

<div class="dashboard-grid">
    <div class="card">
        <div class="card-header">
            <h2 class="card-title">Welcome back,
                <?php echo htmlspecialchars($_SESSION['admin_name'] ?? $_SESSION['admin_username']); ?>!
            </h2>
        </div>
        <p>This is your administration panel where you can manage your website content, services, reviews, and more.</p>
        <div class="dashboard-actions">
            <a href="/admin/services" class="btn btn-primary">Manage Services</a>
            <a href="/admin/appointments" class="btn btn-secondary">View Appointments</a>
            <a href="/admin/profile" class="btn btn-secondary">Update Profile</a>
        </div>
    </div>

    <div class="card">
        <div class="card-header">
            <h2 class="card-title">Quick Actions</h2>
        </div>
        <div class="quick-actions-list">
            <a href="/admin/appointments" class="nav-link">
                <i class="fas fa-calendar-alt"></i> Manage Appointments
            </a>
            <a href="/admin/settings" class="nav-link">
                <i class="fas fa-cog"></i> General Settings
            </a>
            <a href="/admin/reviews" class="nav-link">
                <i class="fas fa-star"></i> Manage Reviews
            </a>
            <a href="/admin/faq" class="nav-link">
                <i class="fas fa-question-circle"></i> Update FAQs
            </a>
        </div>
    </div>
</div>

<?php require_once 'includes/admin-footer.php'; ?>