<?php
session_start();
require_once "config/database.php";

if (!isset($_SESSION['user_id']) || ($_SESSION['role'] ?? '') !== 'admin') {
    header("Location: login.php");
    exit;
}

$database = new Database();
$db = $database->getConnection();

if (!$db) {
    die("Database connection failed.");
}

$total_users = 0;

try {
    $stmt = $db->prepare("SELECT COUNT(*) AS total FROM users");
    $stmt->execute();
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    $total_users = (int)($row['total'] ?? 0);
} catch (PDOException $e) {
    die("Query error: " . $e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Total Users</title>
    <link rel="stylesheet" href="assets/css/app.css">
</head>
<body>
<div class="layout">
    <?php include 'nav.php'; ?>

    <main class="main">
        <div class="hero">
            <div class="hero-top">
                <div>
                    <h1>User Overview</h1>
                    <p>Only admin can see this page.</p>
                </div>
                <div class="welcome-badge">
                    Admin
                </div>
            </div>
        </div>

        <div class="card">
            <div class="section-title">Registered Users</div>
            <div class="section-subtext">Total number of users in the system.</div>

            <div class="summary-grid">
                <div class="summary-box">
                    <div class="label">Total Users</div>
                    <div class="value"><?php echo $total_users; ?></div>
                </div>
            </div>
        </div>
    </main>
</div>
</body>
</html>