<?php
require_once __DIR__ . '/../../app/helpers/rbac.php';

$menu = require __DIR__ . '/../../config/menu.php';
$current = basename($_SERVER['PHP_SELF']);
$requestUri = $_SERVER['REQUEST_URI'] ?? '';
$currentLink = ltrim((string)parse_url($requestUri, PHP_URL_PATH), '/');
$currentQuery = (string)parse_url($requestUri, PHP_URL_QUERY);

function menuItemAllowed(array $item): bool
{
    if (isAdmin()) {
        return true;
    }

    $permission = $item['permission'] ?? null;
    return empty($permission) || can($permission);
}

function menuItemMatches(array $item, string $current, string $currentLink, string $currentQuery): bool
{
    $link = $item['link'] ?? '';
    if ($link === '') {
        return false;
    }

    $linkPath = basename((string)parse_url($link, PHP_URL_PATH));
    $linkQuery = (string)parse_url($link, PHP_URL_QUERY);

    if ($linkPath !== '' && $linkPath === $current) {
        if ($linkQuery === '' || $linkQuery === $currentQuery) {
            return true;
        }
    }

    return ltrim((string)parse_url($link, PHP_URL_PATH), '/') === $currentLink && ($linkQuery === '' || $linkQuery === $currentQuery);
}

function menuGroupVisible(array $item): bool
{
    if (empty($item['children']) || !is_array($item['children'])) {
        return menuItemAllowed($item);
    }

    foreach ($item['children'] as $child) {
        if (menuItemAllowed($child)) {
            return true;
        }
    }

    return menuItemAllowed($item);
}

function menuGroupActive(array $item, string $current, string $currentLink, string $currentQuery): bool
{
    if (menuItemMatches($item, $current, $currentLink, $currentQuery)) {
        return true;
    }

    foreach (($item['children'] ?? []) as $child) {
        if (menuItemMatches($child, $current, $currentLink, $currentQuery)) {
            return true;
        }
    }

    return false;
}
?>

<aside class="sidebar bg-dark text-white p-3" id="sidebar">

    <h5 class="mb-4">FleetOps</h5>

    <nav class="nav flex-column">

        <?php foreach ($menu as $item): ?>

            <?php if (($item['type'] ?? '') === 'section'): ?>
                <div class="text-uppercase text-muted small mt-3 mb-1 px-2">
                    <?= $item['label'] ?>
                </div>
                <?php continue; ?>
            <?php endif; ?>

            <?php if (menuGroupVisible($item)): ?>
                <?php if (!empty($item['children']) && is_array($item['children'])): ?>
                    <?php
                    $groupId = 'menu_' . md5((string)$item['title']);
                    $groupActive = menuGroupActive($item, $current, $currentLink, $currentQuery);
                    ?>
                    <button
                        class="nav-link nav-link-toggle text-white d-flex align-items-center justify-content-between gap-2 px-2 py-2 rounded border-0 w-100 <?= $groupActive ? 'bg-primary' : '' ?>"
                        type="button"
                        data-bs-toggle="collapse"
                        data-bs-target="#<?= $groupId ?>"
                        aria-expanded="<?= $groupActive ? 'true' : 'false' ?>"
                        aria-controls="<?= $groupId ?>"
                    >
                        <span class="d-flex align-items-center gap-2">
                            <i class="bi <?= $item['icon'] ?>"></i>
                            <span><?= $item['title'] ?></span>
                        </span>
                        <i class="bi bi-chevron-down submenu-chevron"></i>
                    </button>

                    <div class="collapse submenu-wrap <?= $groupActive ? 'show' : '' ?>" id="<?= $groupId ?>">
                        <?php foreach ($item['children'] as $child): ?>
                            <?php if (menuItemAllowed($child)): ?>
                                <?php $childActive = menuItemMatches($child, $current, $currentLink, $currentQuery); ?>
                                <a href="<?= $child['link'] ?>"
                                   class="nav-link submenu-link text-white d-flex align-items-center gap-2 px-2 py-2 rounded <?= $childActive ? 'bg-primary' : '' ?>">
                                    <i class="bi <?= $child['icon'] ?>"></i>
                                    <?= $child['title'] ?>
                                </a>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </div>
                <?php else: ?>
                    <a href="<?= $item['link'] ?>"
                       class="nav-link text-white d-flex align-items-center gap-2 px-2 py-2 rounded <?= menuItemMatches($item, $current, $currentLink, $currentQuery) ? 'bg-primary' : '' ?>">
                        <i class="bi <?= $item['icon'] ?>"></i>
                        <?= $item['title'] ?>
                    </a>
                <?php endif; ?>
            <?php endif; ?>

        <?php endforeach; ?>

    </nav>

</aside>
