<?php
// Required variables: $baseUrl, $project, $user
$__navStandalone = \PpPhp\Core\ProjectDatabaseProfile::isStandaloneSlug((string)($project['slug'] ?? ''));
$spaPages = \PpPhp\Core\ProjectNav::spaPages($__navStandalone);
?>
<!-- Top Navbar (responsive, with integrated project toolbar) -->
<nav class="navbar navbar-top navbar-dark bg-dark px-2 px-md-3 fixed-top shadow-sm" id="projectNavbar"
    data-base-url="<?= htmlspecialchars((string)$baseUrl) ?>"
    data-project-slug="<?= htmlspecialchars((string)($project['slug'] ?? '')) ?>">
    <!-- Left side: sidebar toggle + brand -->
    <div class="d-flex align-items-center">
        <span class="sidebar-toggle d-none d-lg-block me-2 me-md-3" id="sidebarToggle" title="Toggle Sidebar" style="cursor:pointer;">
            <i class="bi bi-layout-sidebar-inset text-white fs-5"></i>
        </span>
        <!-- Mobile menu: opens the native left drawer (project-mobile-nav.php). -->
        <button class="btn btn-dark d-lg-none me-2" type="button" data-ppm-open aria-label="Open menu" aria-controls="ppmDrawer">
            <i class="bi bi-list fs-4"></i>
        </button>
        <a class="navbar-brand d-flex align-items-center" href="<?= $baseUrl ?>/projects">
            <!-- Mobile: the full PPPHP lockup (compact). Desktop: monogram + project name (unchanged). -->
            <span class="d-md-none d-inline-flex align-items-center"><?= \PpPhp\Core\Brand::lockup(24, 'logo-svg flex-shrink-0') ?></span>
            <span class="d-none d-md-flex align-items-center">
                <?= \PpPhp\Core\Brand::mark(30, 'logo-svg flex-shrink-0') ?>
                <span class="ms-2 text-truncate" style="max-width:120px;"><?= htmlspecialchars($project['name'] ?? 'Project') ?></span>
            </span>
        </a>
    </div>

    <?php
    // Compact "current screen" pill — mobile only, centered. Tap to open the nav drawer.
    $__curPath = (string) parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH);
    $__curUri  = '';
    if (preg_match('#/project/[^/]+(/[a-z0-9-]+)#i', $__curPath, $__cm)) { $__curUri = strtolower($__cm[1]); }
    $__curLabel = 'Workspace'; $__curIcon = 'grid-1x2';
    foreach ($spaPages as $__p) {
        if (strtolower((string)($__p['uri'] ?? '')) === $__curUri) { $__curLabel = (string)$__p['label']; $__curIcon = (string)$__p['icon']; break; }
    }
    ?>
    <button type="button" class="pp-cur-page d-md-none" data-ppm-open aria-controls="ppmDrawer" aria-label="Current screen: <?= htmlspecialchars($__curLabel) ?>. Tap to switch.">
        <i class="bi bi-<?= htmlspecialchars($__curIcon) ?>"></i>
        <span class="pp-cur-page-t"><?= htmlspecialchars($__curLabel) ?></span>
        <i class="bi bi-chevron-down pp-cur-page-c"></i>
    </button>

    <!-- Project Toolbar – visible on medium+ screens, collapsed into dropdown on small -->
    <div class="d-flex align-items-center gap-1 gap-md-2">
        <!-- Desktop toolbar (hidden on small) -->
        <div class="d-none d-md-flex align-items-center" id="projectToolbarDesktop">

            <!-- Screen navigation (segmented) -->
            <div class="btn-group btn-group-sm pp-nav-group" role="group" aria-label="Screen navigation">
                <button class="btn btn-outline-light px-2" id="btnPrevPage" data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Previous screen" aria-label="Previous screen"><i class="bi bi-chevron-left"></i></button>
                <button class="btn btn-outline-light dropdown-toggle pp-page-btn" type="button" id="spaPageDropdown" data-bs-toggle="dropdown" data-bs-auto-close="outside" aria-expanded="false" title="Switch to another screen">
                    <i class="bi bi-grid-1x2 pp-page-btn-ico" id="currentPageIcon" aria-hidden="true"></i>
                    <span id="currentPageLabel">Workspace</span>
                </button>
                <div class="dropdown-menu pp-spa-menu" id="spaPageMenu" aria-labelledby="spaPageDropdown">
                    <div class="pp-spa-search">
                        <i class="bi bi-search" aria-hidden="true"></i>
                        <input type="text" id="spaPageSearch" class="pp-spa-input" placeholder="Jump to a screen…" autocomplete="off" spellcheck="false" aria-label="Filter screens">
                        <kbd class="pp-spa-kbd">esc</kbd>
                    </div>
                    <div class="pp-spa-list" role="menu">
                        <?php $__grp = null; foreach ($spaPages as $page): ?>
                            <?php if (($page['group'] ?? '') !== $__grp): $__grp = (string)($page['group'] ?? ''); ?>
                                <div class="pp-spa-group" data-group aria-hidden="true"><?= htmlspecialchars($__grp) ?></div>
                            <?php endif; ?>
                            <?php if (!empty($page['locked'])): ?>
                                <span class="pp-spa-item is-locked" role="menuitem" aria-disabled="true" title="Needs a database — this is a standalone app" data-label="<?= htmlspecialchars($page['label']) ?>">
                                    <span class="pp-spa-ico"><i class="bi bi-<?= htmlspecialchars($page['icon']) ?>" aria-hidden="true"></i></span>
                                    <span class="pp-spa-lbl"><?= htmlspecialchars($page['label']) ?></span>
                                    <i class="bi bi-lock-fill pp-spa-lock" aria-hidden="true"></i>
                                </span>
                            <?php else: ?>
                                <a class="pp-spa-item" href="#" role="menuitem" data-page="<?= htmlspecialchars($page['uri']) ?>" data-label="<?= htmlspecialchars($page['label']) ?>" data-icon="<?= htmlspecialchars($page['icon']) ?>" data-key="<?= htmlspecialchars($page['key']) ?>">
                                    <span class="pp-spa-ico"><i class="bi bi-<?= htmlspecialchars($page['icon']) ?>" aria-hidden="true"></i></span>
                                    <span class="pp-spa-lbl"><?= htmlspecialchars($page['label']) ?></span>
                                    <span class="pp-spa-kbd-combo" aria-hidden="true"><kbd class="pp-spa-mod" data-mod-combo>&#8963;&#8997;</kbd><kbd><?= htmlspecialchars($page['key']) ?></kbd></span>
                                    <i class="bi bi-check2 pp-spa-check" aria-hidden="true"></i>
                                </a>
                            <?php endif; ?>
                        <?php endforeach; ?>
                        <div class="pp-spa-empty" hidden>No screens match.</div>
                    </div>
                    <div class="pp-spa-foot" aria-hidden="true">
                        <span><kbd><span data-mod-key>&#8984;</span>K</kbd> open</span>
                        <span><kbd data-mod-combo>&#8963;&#8997;</kbd> + letter to jump</span>
                        <span><kbd>&#8629;</kbd> go</span>
                    </div>
                </div>
                <button class="btn btn-outline-light px-2" id="btnNextPage" data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Next screen" aria-label="Next screen"><i class="bi bi-chevron-right"></i></button>
            </div>

            <span class="pp-toolbar-sep" aria-hidden="true"></span>

            <button class="btn btn-sm btn-outline-light pp-icon-btn" type="button" data-vh-open data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Version History — step back to an earlier version" aria-label="Version History"><i class="bi bi-clock-history"></i></button>
            <button class="btn btn-sm btn-success ms-2" id="btnSaveProject" data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Save your latest changes"><i class="bi bi-save"></i><span class="d-none d-lg-inline ms-1">Save</span></button>
            <button class="btn btn-sm btn-primary ms-1" id="btnBuildProject" data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Generate your application from this project"><i class="bi bi-hammer"></i><span class="d-none d-lg-inline ms-1">Build</span></button>

            <span class="pp-toolbar-sep" aria-hidden="true"></span>

            <a class="btn btn-sm btn-outline-light" href="<?= $baseUrl ?>/projects" id="btnCloseProject" data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Save &amp; close — back to My Projects"><i class="bi bi-x-lg"></i><span class="d-none d-lg-inline ms-1">Close</span></a>
        </div>

        <!-- Mobile toolbar dropdown (visible only on small) -->
        <div class="dropdown d-md-none">
            <button class="btn btn-sm btn-outline-light" type="button" id="mobileToolbarDropdown" data-bs-toggle="dropdown" aria-expanded="false">
                <i class="bi bi-gear-fill"></i>
            </button>
            <ul class="dropdown-menu dropdown-menu-dark dropdown-menu-end" aria-labelledby="mobileToolbarDropdown">
                <li><span class="dropdown-item-text small text-muted mb-0">Navigate</span></li>
                <?php foreach ($spaPages as $page): ?>
                    <?php if (!empty($page['locked'])): ?>
                        <li><span class="dropdown-item disabled d-flex align-items-center" style="opacity:.5;cursor:not-allowed" data-nav="<?= htmlspecialchars($page['uri']) ?>" title="Needs a database — this is a standalone app"><i class="bi bi-<?= htmlspecialchars($page['icon']) ?> me-2 pp-nav-ico"></i><?= htmlspecialchars($page['label']) ?><i class="bi bi-lock-fill ms-auto opacity-75" style="font-size:.8em"></i></span></li>
                    <?php else: ?>
                        <li><a class="dropdown-item mobile-nav-item" href="#" data-page="<?= htmlspecialchars($page['uri']) ?>" data-nav="<?= htmlspecialchars($page['uri']) ?>"><i class="bi bi-<?= htmlspecialchars($page['icon']) ?> me-2 pp-nav-ico"></i><?= htmlspecialchars($page['label']) ?></a></li>
                    <?php endif; ?>
                <?php endforeach; ?>
                <li><hr class="dropdown-divider"></li>
                <li><a class="dropdown-item" href="#" data-vh-open><i class="bi bi-clock-history me-2"></i>Version History</a></li>
                <li><a class="dropdown-item" href="#" id="mobileBtnSave"><i class="bi bi-save me-2"></i>Save</a></li>
                <li><a class="dropdown-item" href="#" id="mobileBtnBuild"><i class="bi bi-hammer me-2"></i>Build</a></li>
                <li><a class="dropdown-item" href="<?= $baseUrl ?>/projects" id="mobileBtnClose"><i class="bi bi-x-lg me-2"></i>Close project</a></li>
            </ul>
        </div>

        <!-- Last saved message -->
        <small id="lastSavedMsg" class="text-white-50 d-none d-lg-inline ms-2"></small>
    </div>

    <!-- Appearance + user controls (always visible) -->
    <div class="d-flex align-items-center gap-2">
        <?php include constant('BASE_PATH') . '/core/views/command-palette.php'; ?>
        <?php include constant('BASE_PATH') . '/core/views/keyboard-shortcuts.php'; ?>
        <?php include constant('BASE_PATH') . '/core/views/theme-toggle.php'; ?>
        <div class="dropdown">
            <a href="#" onclick="return false;" class="d-flex align-items-center text-white text-decoration-none dropdown-toggle" data-bs-toggle="dropdown">
                <i class="bi bi-person-circle me-1 me-md-2"></i><span class="d-none d-sm-inline"><?= htmlspecialchars($user['username'] ?? 'Admin') ?></span>
            </a>
            <ul class="dropdown-menu dropdown-menu-dark dropdown-menu-end">
                <?php if (\PpPhp\Core\Security::IsAdmin()): /* superadmin + owner */ ?>
                    <li><a class="dropdown-item" href="<?= $baseUrl ?>/users"><i class="bi bi-people me-2"></i>Manage Users</a></li>
                    <li><a class="dropdown-item" href="<?= $baseUrl ?>/platform/updates"><i class="bi bi-arrow-repeat me-2"></i>Updates</a></li>
                    <?php include constant('BASE_PATH') . '/core/views/license-menu.php'; ?>
                    <li><hr class="dropdown-divider"></li>
                <?php endif; ?>
                <li><a class="dropdown-item" href="<?= $baseUrl ?>/profile"><i class="bi bi-person me-2"></i>Profile</a></li>
                <li><a class="dropdown-item" href="<?= $baseUrl ?>/account/security"><i class="bi bi-shield-lock me-2"></i>Security &amp; 2FA</a></li>
                <li><hr class="dropdown-divider"></li>
                <li><a class="dropdown-item" href="<?= $baseUrl ?>/logout"><i class="bi bi-box-arrow-right me-2"></i>Sign out</a></li>
            </ul>
        </div>
    </div>
</nav>

<?php include constant('BASE_PATH') . '/core/views/version-history-panel.php'; ?>
<?php include constant('BASE_PATH') . '/core/views/project-mobile-nav.php'; ?>
