<?php

declare(strict_types=1);

require_once __DIR__ . '/../../core/web_bootstrap.php';
require_once __DIR__ . '/../../core/permissions.php';

/* ----------------------------------------------------------
   SESSION CHECK
---------------------------------------------------------- */

if (empty($_SESSION['user'])) {
    header('Location: login.php');
    exit;
}

$user = $_SESSION['user'];

/* ----------------------------------------------------------
   FETCH VISIBLE MODULES (STRICT RBAC)
---------------------------------------------------------- */

$stmt = $pdo->prepare("
    SELECT 
        m.module_key,
        m.module_name,
        m.nav_label,
        m.route,
        COALESCE(m.nav_group, 'General') AS nav_group,
        m.nav_order
    FROM modules m
    INNER JOIN role_permissions rp
        ON rp.module_key = m.module_key
    WHERE m.is_active = 1
        AND rp.role = ?
        AND rp.can_view = 1
    ORDER BY m.nav_group ASC, m.nav_order ASC
");

$stmt->execute([$user['role']]);

$modules = $stmt->fetchAll(PDO::FETCH_ASSOC);

/* ----------------------------------------------------------
   GROUP MODULES
---------------------------------------------------------- */

$grouped = [];

foreach ($modules as $module) {
    $grouped[$module['nav_group']][] = $module;
}

$current = basename($_SERVER['PHP_SELF']);

/* ----------------------------------------------------------
   ACTIVE HELPERS
---------------------------------------------------------- */

function isActive(string $route, string $current): bool
{
    return basename($route) === $current;
}

function isGroupActive(array $items, string $current): bool
{
    foreach ($items as $item) {
        if (basename($item['route']) === $current) {
            return true;
        }
    }
    return false;
}

?>

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">

<a class="navbar-brand" href="dashboard.php">
    Assist Admin
</a>

<button class="navbar-toggler" type="button"
    data-bs-toggle="collapse"
    data-bs-target="#navbarNav"
    aria-controls="navbarNav"
    aria-expanded="false"
    aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse" id="navbarNav">

<ul class="navbar-nav me-auto">

<?php if (!empty($grouped)): ?>

    <?php foreach ($grouped as $group => $items): ?>

        <?php $groupActive = isGroupActive($items, $current); ?>

        <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle <?= $groupActive ? 'active' : '' ?>"
               href="#"
               role="button"
               data-bs-toggle="dropdown"
               aria-expanded="false">
               <?= htmlspecialchars($group, ENT_QUOTES, 'UTF-8') ?>
            </a>

            <ul class="dropdown-menu">

                <?php foreach ($items as $item): ?>
                    <li>
                        <a class="dropdown-item <?= isActive($item['route'], $current) ? 'active' : '' ?>"
                           href="<?= htmlspecialchars($item['route'], ENT_QUOTES, 'UTF-8') ?>">
                           <?= htmlspecialchars($item['nav_label'], ENT_QUOTES, 'UTF-8') ?>
                        </a>
                    </li>
                <?php endforeach; ?>

            </ul>
        </li>

    <?php endforeach; ?>

<?php endif; ?>

</ul>

<ul class="navbar-nav">

    <li class="nav-item">
        <span class="navbar-text text-light me-3">
            <?= htmlspecialchars($user['first_name'] . ' ' . $user['last_name'], ENT_QUOTES, 'UTF-8') ?>
            (<?= htmlspecialchars($user['role'], ENT_QUOTES, 'UTF-8') ?>)
        </span>
    </li>

    <li class="nav-item">
        <a class="nav-link text-danger" href="logout.php">
            Logout
        </a>
    </li>

</ul>

</div>
</div>
</nav>