<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) {
    header('Location: login.php');
    exit;
}
require_once '../src/config/db.php';
require_once '../src/includes/functions.php';
require_once 'includes/lang.php';

// Ensure all required tables exist before querying them to prevent fatal errors.
ensure_certifications_table($pdo);
ensure_contact_messages_table($pdo);
ensure_sliders_table($pdo);
ensure_services_table($pdo);
ensure_pages_table($pdo);
ensure_users_table($pdo);

$servicesCount = $pdo->query("SELECT COUNT(*) FROM services")->fetchColumn();
$pagesCount    = $pdo->query("SELECT COUNT(*) FROM pages")->fetchColumn();
$usersCount    = $pdo->query("SELECT COUNT(*) FROM users")->fetchColumn();
$certificationsCount = $pdo->query("SELECT COUNT(*) FROM certifications")->fetchColumn();
$unreadMessagesCount = $pdo->query("SELECT COUNT(*) FROM contact_messages WHERE is_read = 0")->fetchColumn();

?>
<!DOCTYPE html>
<html lang="<?php echo $t['lang']; ?>" dir="<?php echo $t['dir']; ?>">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $t['admin_dashboard']; ?> - VAULTIXAI</title>
    <link rel="stylesheet" href="<?php echo $bootstrapCSS; ?>" crossorigin="anonymous">
    <link rel="stylesheet" href="assets/css/style.css">
    <script>
        // Apply admin theme ASAP to prevent FOUC
        const adminStoredTheme = localStorage.getItem('admin_theme') || 'light';
        document.documentElement.setAttribute('data-theme', adminStoredTheme);
    </script>
</head>
<body>

    <?php
    $currentPage = 'dashboard';
    include 'includes/sidebar.php';
    ?>

    <!-- ═══ MAIN CONTENT ════════════════════════════════════════ -->
    <div class="main-content">

        <!-- Top Bar -->
        <div class="topbar">
            <h1 class="me-auto"><?php echo $t['admin_dashboard_title']; ?></h1>
            <div class="d-flex align-items-center gap-3">
                <div class="admin-badge">
                    👋 <?php echo $t['admin_welcome']; ?>
                    <strong class="text-dark"><?php echo htmlspecialchars($_SESSION['admin_name'] ?? 'Admin'); ?></strong>
                </div>
                <button id="admin-theme-toggle" class="admin-theme-toggle" title="Toggle theme">
                    <span class="theme-icon">🌙</span>
                </button>
            </div>
        </div>

        <!-- Stat Cards -->
        <div class="row g-4 mb-4">
            <div class="col-12 col-md-6 col-lg-3">
                <a href="messages.php" class="stat-card messages text-decoration-none">
                    <span class="stat-icon">✉️</span>
                    <div class="stat-number"><?php echo $unreadMessagesCount; ?></div>
                    <div class="stat-label"><?php echo $t['admin_new_messages_stat']; ?></div>
                    <div class="stat-link"><?php echo $t['admin_manage_messages']; ?></div>
                </a>
            </div>

            <div class="col-12 col-md-6 col-lg-3">
                <a href="services.php" class="stat-card services text-decoration-none">
                    <span class="stat-icon">⚙️</span>
                    <div class="stat-number"><?php echo $servicesCount; ?></div>
                    <div class="stat-label"><?php echo $t['admin_total_services']; ?></div>
                    <div class="stat-link"><?php echo $t['admin_manage_services']; ?></div>
                </a>
            </div>

            <div class="col-12 col-md-6 col-lg-3">
                <a href="certifications.php" class="stat-card certifications text-decoration-none">
                    <span class="stat-icon">🏆</span>
                    <div class="stat-number"><?php echo $certificationsCount; ?></div>
                    <div class="stat-label"><?php echo $t['certs_title']; ?></div>
                    <div class="stat-link"><?php echo $t['admin_manage_certs']; ?></div>
                </a>
            </div>

            <div class="col-12 col-md-6 col-lg-3">
                <a href="pages.php" class="stat-card pages text-decoration-none">
                    <span class="stat-icon">📄</span>
                    <div class="stat-number"><?php echo $pagesCount; ?></div>
                    <div class="stat-label"><?php echo $t['admin_total_pages']; ?></div>
                    <div class="stat-link"><?php echo $t['admin_manage_pages']; ?></div>
                </a>
            </div>

            <div class="col-12 col-md-6 col-lg-3">
                <a href="users.php" class="stat-card users text-decoration-none">
                    <span class="stat-icon">👥</span>
                    <div class="stat-number"><?php echo $usersCount; ?></div>
                    <div class="stat-label"><?php echo $t['admin_total_users']; ?></div>
                    <div class="stat-link"><?php echo $t['admin_manage_users']; ?></div>
                </a>
            </div>
        </div>

        <!-- Welcome Card -->
        <div class="welcome-card">
            <h2>
                <img src="../public/assets/img/vous.png" alt="VAULTIXAI Shield" style="height: 1.2em; width: auto; vertical-align: middle; margin-bottom: 0.2em; <?php echo $isRTL ? 'margin-left' : 'margin-right'; ?>: 0.5rem;">
                <?php echo $t['admin_system_title']; ?></h2>
            <p class="mb-4"><?php echo $t['admin_system_desc']; ?></p>
            <div class="d-flex gap-3 flex-wrap">
                <a href="users.php" class="btn btn-gold px-4 py-2">
                    👥 <?php echo $t['admin_users']; ?>
                </a>
                <a href="services.php" class="btn btn-outline-light px-4 py-2">
                    ⚙️ <?php echo $t['admin_services']; ?>
                </a>
                <a href="../public/index.php" target="_blank" class="btn btn-outline-light px-4 py-2">
                    <?php echo $t['admin_visit_site']; ?>
                </a>
            </div>
        </div>

    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
    <script src="assets/js/main.js"></script>
</body>
</html>
