<nav class="navbar navbar-expand-lg navbar-dark bg-primary sticky-top">
    <div class="container">
        <a class="navbar-brand" href="<?php echo getCleanUrl('index'); ?>">
            <?php if (defined('USE_LOGO') && USE_LOGO && defined('LOGO_URL') && !empty(LOGO_URL)): ?>
                <img src="<?php echo LOGO_URL; ?>" alt="<?php echo SITE_NAME; ?>" <?php echo (defined('LOGO_WIDTH') && LOGO_WIDTH) ? 'width="' . LOGO_WIDTH . '"' : ''; ?>>
            <?php else: ?>
                <?php echo SITE_NAME; ?>
            <?php endif; ?>
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav me-auto">
                <!-- Tickets are always displayed -->
                <li class="nav-item">
                    <a class="nav-link text-white"
                        href="<?php echo getCleanUrl('tickets'); ?>"><?php echo $lang_navigation['NAV_TICKETS'] ?? 'Tickets'; ?></a>
                </li>
                <?php if (defined('LIVE_CHAT') && LIVE_CHAT): ?>
                    <li class="nav-item">
                        <a class="nav-link text-white"
                            href="<?php echo getCleanUrl('livechat'); ?>"><?php echo $lang_navigation['NAV_CHAT'] ?? 'Chat'; ?></a>
                    </li>
                <?php endif; ?>
                <?php if (isLoggedIn() && defined('ENABLE_PROJECTS') && ENABLE_PROJECTS): ?>
                    <li class="nav-item">
                        <a class="nav-link text-white"
                            href="<?php echo getCleanUrl('projects'); ?>"><?php echo $lang_navigation['NAV_PROJECTS'] ?? 'Projects'; ?></a>
                    </li>
                <?php endif; ?>
                <?php if (isLoggedIn() && defined('ENABLE_PROJECTS') && ENABLE_PROJECTS && defined('ENABLE_FILES') && ENABLE_FILES): ?>
                    <li class="nav-item">
                        <a class="nav-link text-white"
                            href="<?php echo getCleanUrl('project_files'); ?>"><?php echo $lang_navigation['NAV_FILES'] ?? 'Files'; ?></a>
                    </li>
                <?php endif; ?>
                <?php if (defined('ENABLE_FAQS') && ENABLE_FAQS): ?>
                    <li class="nav-item">
                        <a class="nav-link text-white"
                            href="<?php echo getCleanUrl('faq'); ?>"><?php echo $lang_navigation['NAV_FAQS'] ?? 'FAQs'; ?></a>
                    </li>
                <?php endif; ?>
                <?php if (defined('ENABLE_NEWS') && ENABLE_NEWS): ?>
                    <li class="nav-item">
                        <a class="nav-link text-white"
                            href="<?php echo getCleanUrl('news'); ?>"><?php echo $lang_navigation['NAV_NEWS'] ?? 'News'; ?></a>
                    </li>
                <?php endif; ?>
                <?php if (defined('ENABLE_CONTACT') && ENABLE_CONTACT): ?>
                    <li class="nav-item">
                        <a class="nav-link text-white"
                            href="<?php echo getCleanUrl('contact'); ?>"><?php echo $lang_navigation['NAV_CONTACT'] ?? 'Contact'; ?></a>
                    </li>
                <?php endif; ?>
                <?php if (defined('PAGE_SERVICES') && PAGE_SERVICES): ?>
                    <li class="nav-item">
                        <a class="nav-link text-white"
                            href="<?php echo getCleanUrl('services'); ?>"><?php echo $lang_navigation['NAV_SERVICES'] ?? 'Services'; ?></a>
                    </li>
                <?php endif; ?>
                <?php if (defined('ENABLE_STORE') && ENABLE_STORE): ?>
                    <li class="nav-item">
                        <a class="nav-link text-white"
                            href="<?php echo getCleanUrl('store'); ?>"><?php echo $lang_navigation['NAV_STORE'] ?? 'Store'; ?></a>
                    </li>
                <?php endif; ?>
                <?php if (defined('PAGE_ABOUT') && PAGE_ABOUT): ?>
                    <li class="nav-item">
                        <a class="nav-link text-white"
                            href="<?php echo getCleanUrl('about'); ?>"><?php echo $lang_navigation['NAV_ABOUT'] ?? 'About'; ?></a>
                    </li>
                <?php endif; ?>
                <?php // Custom Header Link - Placed before Admin dropdown for general visibility
                if (defined('CUSTOM_HEADER') && CUSTOM_HEADER && defined('CUSTOM_HEADER_URL') && !empty(CUSTOM_HEADER_URL) && defined('CUSTOM_HEADER_NAME') && !empty(CUSTOM_HEADER_NAME)): ?>
                    <li class="nav-item">
                        <a class="nav-link text-white" href="<?php echo htmlspecialchars(CUSTOM_HEADER_URL); ?>"
                            target="_blank">
                            <?php echo htmlspecialchars(CUSTOM_HEADER_NAME); ?>
                        </a>
                    </li>
                <?php endif; ?>
            </ul>

            <ul class="navbar-nav">
                <?php if (defined('SITE_MULTI_LANGUAGE') && SITE_MULTI_LANGUAGE):
                    $languages = getAvailableLanguages();
                    if (count($languages) > 1): // Only show if more than one language available
                        $currentLang = defined('SITE_LANGUAGE') ? SITE_LANGUAGE : 'English-US';
                        ?>
                        <li class="nav-item dropdown me-2">
                            <a class="nav-link text-white" href="#" id="languageDropdown" role="button"
                                data-bs-toggle="dropdown">
                                <i class="bi bi-flag"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-menu-end">
                                <?php foreach ($languages as $langCode => $langName): ?>
                                    <li>
                                        <?php
                                        // For non-current language, append language code after BASE_URL
                                        $langUrl = $langCode === $currentLang ?
                                            BASE_URL :
                                            rtrim(BASE_URL, '/') . '/' . strtolower(explode('-', $langCode)[1]);
                                        ?>
                                        <a class="dropdown-item <?php echo $langCode === $currentLang ? 'fw-bold' : ''; ?>"
                                            href="<?php echo $langUrl; ?>">
                                            <?php echo $langName; ?>
                                        </a>
                                    </li>
                                <?php endforeach; ?>
                            </ul>
                        </li>
                    <?php endif; endif; ?>
                <?php if (isLoggedIn()): ?>
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle text-white" href="#" id="userDropdown" role="button"
                            data-bs-toggle="dropdown">
                            <?php
                            if (!empty($_SESSION['avatar'])) {
                                echo '<img src="' . getCleanUrl('uploads/avatars/' . $_SESSION['avatar']) . '" class="avatar-sm me-2" alt="Profile">';
                            } else {
                                echo '<i class="bi bi-person-circle me-2"></i>';
                            }
                            echo $_SESSION['name'];
                            ?>
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end">
                            <?php if (isAdmin()): ?>
                                <li><a class="dropdown-item"
                                        href="<?php echo getCleanUrl('admin/dashboard'); ?>"><?php echo $lang_navigation['NAV_DROPDOWN_DASHBOARD_ADMIN'] ?? 'Dashboard'; ?></a>
                                </li>
                            <?php else: ?>
                                <li><a class="dropdown-item"
                                        href="<?php echo getCleanUrl('account'); ?>"><?php echo $lang_navigation['NAV_DROPDOWN_DASHBOARD_USER'] ?? 'Dashboard'; ?></a>
                                </li>
                            <?php endif; ?>
                            <li><a class="dropdown-item"
                                    href="<?php echo getCleanUrl('profile'); ?>"><?php echo $lang_navigation['NAV_DROPDOWN_PROFILE'] ?? 'Profile'; ?></a>
                            </li>
                            <?php if (isAdmin()): ?>
                                <li>
                                    <hr class="dropdown-divider">
                                </li>
                                <li><a class="dropdown-item"
                                        href="<?php echo getCleanUrl('admin/users'); ?>"><?php echo $lang_navigation['NAV_DROPDOWN_USERS_MANAGEMENT'] ?? 'Users management'; ?></a>
                                </li>
                                <li><a class="dropdown-item"
                                        href="<?php echo getCleanUrl('admin/tickets'); ?>"><?php echo $lang_navigation['NAV_DROPDOWN_TICKETS_MANAGEMENT'] ?? 'Tickets management'; ?></a>
                                </li>
                                <li><a class="dropdown-item"
                                        href="<?php echo getCleanUrl('admin/tickets_categories'); ?>"><?php echo $lang_navigation['NAV_DROPDOWN_TICKETS_CATEGORIES'] ?? 'Tickets categories'; ?></a>
                                </li>
                                <li><a class="dropdown-item"
                                        href="<?php echo getCleanUrl('admin/snippets'); ?>"><?php echo $lang_navigation['NAV_DROPDOWN_TICKET_SNIPPETS'] ?? 'Ticket snippets'; ?></a>
                                </li>
                                <li>
                                    <hr class="dropdown-divider">
                                </li>
                                
                                <li><a class="dropdown-item"
                                        href="<?php echo getCleanUrl('admin/settings'); ?>"><?php echo $lang_navigation['NAV_DROPDOWN_SYSTEM_SETTINGS'] ?? 'System Settings'; ?></a>
                                </li>
                                <li><a class="dropdown-item"
                                        href="<?php echo getCleanUrl('debug_info'); ?>"><?php echo $lang_navigation['NAV_DROPDOWN_SYSTEM_INFORMATION'] ?? 'System Information'; ?></a>
                                </li>
                            <?php endif; ?>
                            <li>
                                <hr class="dropdown-divider">
                            </li>
                            <li><a class="dropdown-item"
                                    href="<?php echo getCleanUrl('logout'); ?>"><?php echo $lang_navigation['NAV_DROPDOWN_LOGOUT'] ?? 'Logout'; ?></a>
                            </li>
                        </ul>
                    </li>
                <?php else: ?>
                    <li class="nav-item">
                        <a class="nav-link text-white"
                            href="<?php echo getCleanUrl('login'); ?>"><?php echo $lang_navigation['NAV_LOGIN'] ?? 'Login'; ?></a>
                    </li>
                    <?php if (ALLOW_REGISTER): ?>
                        <li class="nav-item">
                            <a class="nav-link text-white"
                                href="<?php echo getCleanUrl('register'); ?>"><?php echo $lang_navigation['NAV_REGISTER'] ?? 'Register'; ?></a>
                        </li>
                    <?php else: ?>
                        <?php // Custom Registration Link
                                if (defined('CUSTOM_REGISTRATION') && CUSTOM_REGISTRATION && defined('CUSTOM_REGISTRATION_URL') && !empty(CUSTOM_REGISTRATION_URL)): ?>
                            <li class="nav-item">
                                <a class="nav-link text-white" href="<?php echo htmlspecialchars(CUSTOM_REGISTRATION_URL); ?>"
                                    target="_blank">
                                    <?php echo $lang_navigation['NAV_REGISTER'] ?? 'Register'; ?>
                                </a>
                            </li>
                        <?php endif; ?>
                    <?php endif; ?>
                <?php endif; ?>
            </ul>
        </div>
    </div>
</nav>