<?php
declare(strict_types=1);
/**
 * Unified site navbar — included by layout.php, layout_dashboard.php, and standalone pages.
 * Assumes bootstrap.php (and i18n.php) has already been required.
 */

$_navGuest   = !dbnToolsIsAuthenticated();
$_navAuth    = $_navGuest ? null : dbnToolsAuthenticatedUser();
$_navEmail   = (string)($_navAuth['email'] ?? '');
$_navUser    = $_navEmail !== '' ? (strstr($_navEmail, '@', true) ?: $_navEmail) : '';
$_navLang    = dbnToolsCurrentLanguage();
$_navTools   = dbnToolsLaunchedTools($_navLang);
$_navPath    = strtok((string)($_SERVER['REQUEST_URI'] ?? '/'), '?') ?: '/';
$_navOnDash  = str_starts_with($_navPath, '/dashboard');
$_navLoginUrl = dbnToolsMainLoginUrl($_navPath);
$_navScriptPath = str_replace('\\', '/', (string)($_SERVER['SCRIPT_NAME'] ?? ''));
$_navAssetBase = str_contains($_navScriptPath, '/dashboard/') ? '../assets' : 'assets';
?>
<nav class="dbn-nav" role="navigation" aria-label="<?= htmlspecialchars(dbnToolsT('suite_title', $_navLang)) ?>">
    <a class="dbn-nav__brand" href="/dashboard.php">
        <picture>
            <source srcset="<?= htmlspecialchars($_navAssetBase) ?>/images/logo-header.webp" type="image/webp">
            <img class="dbn-nav__brand-logo" src="<?= htmlspecialchars($_navAssetBase) ?>/images/logo-header.png" alt="Do Better Norge" width="140" height="36" loading="eager">
        </picture>
        <span class="dbn-nav__brandmark" aria-hidden="true">⚖</span>
        <span class="dbn-nav__brand-text">
            <span class="dbn-nav__brandname">Do Better Norge</span>
            <span class="dbn-nav__product">Legal Tools</span>
        </span>
    </a>

    <div class="dbn-nav__links" role="menubar">
        <div class="dbn-nav__dropdown" data-nav-dropdown>
            <button class="dbn-nav__link dbn-nav__dropper"
                    type="button"
                    aria-haspopup="menu"
                    aria-expanded="false"
                    aria-controls="navToolsPanel">
                <?= htmlspecialchars(dbnToolsT('nav_tools', $_navLang)) ?>
                <span class="dbn-nav__caret" aria-hidden="true">▾</span>
            </button>
            <div class="dbn-nav__panel" id="navToolsPanel" role="menu" aria-label="<?= htmlspecialchars(dbnToolsT('nav_tools', $_navLang)) ?>">
<?php foreach ($_navTools as $slug => $item): ?>
                <a class="dbn-nav__panel-item" href="<?= htmlspecialchars($item['url']) ?>" role="menuitem">
                    <span class="dbn-nav__panel-badge" aria-hidden="true"><?= htmlspecialchars($item['icon']) ?></span>
                    <span class="dbn-nav__panel-label"><?= htmlspecialchars($item['label']) ?></span>
                    <span class="dbn-nav__panel-sub"><?= htmlspecialchars($item['sub']) ?></span>
                </a>
<?php endforeach; ?>
            </div>
        </div>

        <a class="dbn-nav__link<?= $_navOnDash ? ' is-active' : '' ?>"
           href="/dashboard.php"
           role="menuitem"
           <?= $_navOnDash ? 'aria-current="page"' : '' ?>>
            <?= htmlspecialchars(dbnToolsT('nav_dashboard', $_navLang)) ?>
        </a>

<?php $_navOnMcp = ($_navPath === '/mcp.php'); ?>
        <a class="dbn-nav__link<?= $_navOnMcp ? ' is-active' : '' ?>"
           href="/mcp.php"
           role="menuitem"
           <?= $_navOnMcp ? 'aria-current="page"' : '' ?>>
            MCP
        </a>
    </div>

    <div class="dbn-nav__right">
        <nav class="dbn-nav__langs" aria-label="Language">
<?php
    $_navQueryBase = $_GET;
    foreach (dbnToolsSupportedLanguages() as $lc):
        $_navQueryBase['lang'] = $lc;
        $_navLangHref = $_navPath . '?' . http_build_query($_navQueryBase);
?>
            <a href="<?= htmlspecialchars($_navLangHref) ?>"
               class="dbn-nav__lang<?= $lc === $_navLang ? ' is-active' : '' ?>"
               hreflang="<?= htmlspecialchars($lc) ?>"
               aria-label="<?= htmlspecialchars(dbnToolsLanguageName($lc)) ?>"
               title="<?= htmlspecialchars(dbnToolsLanguageName($lc)) ?>"><?= htmlspecialchars(strtoupper($lc)) ?></a>
<?php endforeach; ?>
        </nav>

<?php if ($_navGuest): ?>
        <a href="<?= htmlspecialchars($_navLoginUrl) ?>" class="dbn-nav__login">
            <?= htmlspecialchars(dbnToolsT('nav_login', $_navLang)) ?>
        </a>
<?php else: ?>
        <a class="dbn-nav__account-link" href="/dashboard.php#account" title="<?= htmlspecialchars($_navEmail) ?>">
<?php if ($_navUser !== ''): ?>
            <span class="dbn-nav__username"><?= htmlspecialchars($_navUser) ?></span>
<?php endif; ?>
            <span class="dbn-nav__account-badge"><?= htmlspecialchars(dbnToolsT('nav_account', $_navLang)) ?></span>
        </a>
        <a href="/api/logout.php" class="dbn-nav__logout">
            <?= htmlspecialchars(dbnToolsT('nav_logout', $_navLang)) ?>
        </a>
<?php endif; ?>
    </div>
</nav>
<script>
(function () {
    var dropdowns = document.querySelectorAll('[data-nav-dropdown]');
    dropdowns.forEach(function (dd) {
        var btn = dd.querySelector('.dbn-nav__dropper');
        var panel = dd.querySelector('.dbn-nav__panel');
        if (!btn || !panel) return;

        function open() { dd.classList.add('is-open'); btn.setAttribute('aria-expanded', 'true'); }
        function close() { dd.classList.remove('is-open'); btn.setAttribute('aria-expanded', 'false'); }
        function toggle() { dd.classList.contains('is-open') ? close() : open(); }

        btn.addEventListener('click', function (e) { e.stopPropagation(); toggle(); });
        document.addEventListener('click', function (e) {
            if (!dd.contains(e.target)) close();
        });
        document.addEventListener('keydown', function (e) {
            if (e.key === 'Escape') close();
        });
    });
}());
</script>
