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

if (isset($_SESSION['user_id'])) {
    $dash = in_array($_SESSION['user_role'] ?? '', ['student', 'parent']) ? 'portal/dashboard.php' : 'admin/dashboard.php';
    header("Location: $dash");
    exit;
}

// ── Visitor counter ───────────────────────────────────────────────────────────
$pdo->exec("CREATE TABLE IF NOT EXISTS visitors (
    id INT AUTO_INCREMENT PRIMARY KEY,
    ip_address VARCHAR(45) NOT NULL,
    visit_date DATE NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY unique_visit (ip_address, visit_date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");

$ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
$today = date('Y-m-d');

$stmt = $pdo->prepare("INSERT IGNORE INTO visitors (ip_address, visit_date) VALUES (?, ?)");
$stmt->execute([$ip, $today]);

$totalVisitors = $pdo->query("SELECT COUNT(DISTINCT ip_address) FROM visitors")->fetchColumn();
$todayVisitors = $pdo->query("SELECT COUNT(DISTINCT ip_address) FROM visitors WHERE visit_date = CURDATE()")->fetchColumn();
$onlineVisitors = $pdo->query("SELECT COUNT(DISTINCT ip_address) FROM visitors WHERE created_at >= NOW() - INTERVAL 5 MINUTE")->fetchColumn();

// ── Public stats ──────────────────────────────────────────────────────────────
$publicStats = $pdo->query("
    SELECT
        (SELECT COUNT(*) FROM schools)  AS schools,
        (SELECT COUNT(*) FROM students) AS students,
        (SELECT COUNT(*) FROM teachers) AS teachers,
        (SELECT COUNT(*) FROM classes)  AS classes
")->fetch();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nukunu Mofiala - Gestion Multi-École</title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body>
    <nav class="navbar">
        <div class="nav-brand">
            <i class="fas fa-school"></i>
            <span>Nukunu Mofiala</span>
        </div>
        <div class="nav-links">
            <a href="index.php" class="active"><i class="fas fa-home"></i> Accueil</a>
            <a href="auth/login.php"><i class="fas fa-sign-in-alt"></i> Connexion</a>
        </div>
    </nav>

    <header class="hero">
        <div class="hero-content">
            <h1>Bienvenue sur Nukunu Mofiala</h1>
            <p>Système de gestion multi-école moderne et efficace</p>
            <a href="auth/login.php" class="btn-primary"><i class="fas fa-sign-in-alt"></i> Se connecter</a>
        </div>
    </header>

    <div class="hero-image">
        <img src="assets/images/<?= rawurlencode('Ouverture de porte.gif') ?>" alt="Ouverture de porte">
    </div>

    <section class="stats">
        <div class="container">
            <div class="stat-card">
                <i class="fas fa-school"></i>
                <h3><?= (int)$publicStats['schools'] ?></h3>
                <p>Écoles</p>
            </div>
            <div class="stat-card">
                <i class="fas fa-user-graduate"></i>
                <h3><?= (int)$publicStats['students'] ?></h3>
                <p>Élèves</p>
            </div>
            <div class="stat-card">
                <i class="fas fa-chalkboard-teacher"></i>
                <h3><?= (int)$publicStats['teachers'] ?></h3>
                <p>Enseignants</p>
            </div>
            <div class="stat-card">
                <i class="fas fa-users"></i>
                <h3><?= (int)$publicStats['classes'] ?></h3>
                <p>Classes</p>
            </div>
        </div>
    </section>

    <footer class="footer">
        <div class="footer-visitors-bar">
            <div class="fv-item">
                <i class="fas fa-eye"></i>
                <span class="fv-nb"><?= (int)$totalVisitors ?></span>
                <span class="fv-lb">Visiteurs</span>
            </div>
            <div class="fv-item">
                <i class="fas fa-calendar-day"></i>
                <span class="fv-nb"><?= (int)$todayVisitors ?></span>
                <span class="fv-lb">Aujourd'hui</span>
            </div>
            <div class="fv-item">
                <i class="fas fa-user-clock"></i>
                <span class="fv-nb"><?= (int)$onlineVisitors ?></span>
                <span class="fv-lb">En ligne</span>
            </div>
        </div>
        <p>&copy; <?= date('Y') ?> Nukunu Mofiala. Tous droits réservés.</p>
    </footer>

    <script src="assets/js/main.js"></script>
</body>
</html>
