<?php
$currentScript = $_SERVER['PHP_SELF'] ?? '';
$currentPage = basename($currentScript);
$scriptDir = trim(dirname($currentScript), '/');
$segmentCount = $scriptDir === '' ? 0 : substr_count($scriptDir, '/') + 1;
$rootPrefix = $segmentCount ? str_repeat('../', $segmentCount) : '';
?>

<!-- Mobile Topbar -->
<div id="mobile-topbar" class="d-lg-none d-flex justify-content-between align-items-center px-3 shadow-sm bg-success text-white">
    <h4 class="m-0 fw-bold">DSP ADMIN</h4>
    <button class="btn btn-outline-light rounded" onclick="toggleSidebar()">
        <i class="fas fa-bars"></i>
    </button>
</div>

<!-- Mobile overlay -->
<div id="overlay" onclick="toggleSidebar()"></div>

<!-- Sidebar -->
<nav class="sidebar d-lg-block shadow">
    <!-- Desktop only header -->
    <div class="admin-header d-none d-lg-block text-center">
        ADMIN-DAC STAFF
    </div>

    <ul class="nav flex-column py-0">
        <li class="nav-item mt-0 border-top border-secondary pt-2">
        <?php
        $sections = [
            'Data Management' => [
                'admin/dashboard.php' => ['Dashboard', 'fa-tachometer-alt'],
                'admin/manage_users.php' => ['Manage Users', 'fa-users'],
                'admin/manage_datasources.php' => ['Manage Data Sources', 'fa-database'],
                'admin/manage_permissions_admin.php' => ['Data Permissions', 'fa-user-lock'],
                'admin/manage_classifications.php' => ['Classifications', 'fa-tags'],
            ],
            'Content Management' => [
                'admin/manage_announcements.php' => ['Announcements', 'fa-bullhorn'],
                'admin/manage_faq.php' => ['FAQ', 'fa-question-circle'],
                'admin/manage_slides.php' => ['Slides', 'fa-images'],
                'admin/manage_aboutus.php' => ['About Us', 'fa-info-circle'],
                'admin/manage_contactus.php' => ['Contact Us', 'fa-envelope'],
            ],
            'Resources' => [
                'user_guide.php' => ['User Guide', 'fa-book-open'],
                'install_config.php' => ['Install & Config', 'fa-tools'],
                'admin/app_log.php' => ['Application Log', 'fa-scroll'],
            ],
            'Account' => [
                'profile.php' => ['My Profile', 'fa-id-card'],
            ],
        ];

        if (!empty($_SESSION['can_run_r'])) {
            $sections['Data Management']['admin/r_in_jupyter.php'] = ['R in JupyterHub', 'fa-code'];
        }

        echo '<style>
                .nav-section {
                    background: rgba(255, 255, 255, 0.04);
                    border: 1px solid rgba(255,255,255,0.06);
                    border-radius: 10px;
                    padding: 0.9rem 0.75rem;
                    margin: 0 0.75rem 1.2rem 0.75rem;
                }
                .nav-section legend {
                    font-size: 0.72rem;
                    letter-spacing: 0.08em;
                    text-transform: uppercase;
                    color: rgba(255,255,255,0.55);
                    width: auto;
                    padding: 0 0.4rem;
                    margin-left: 0.35rem;
                }
              </style>';

        foreach ($sections as $sectionLabel => $items) {
            echo '<fieldset class="nav-section"><legend>' . htmlspecialchars($sectionLabel) . '</legend>';
            foreach ($items as $file => [$title, $icon]) {
                $targetPath = parse_url($file, PHP_URL_PATH);
                $targetPath = ($targetPath === null || $targetPath === false) ? $file : $targetPath;
                $active = ($currentPage === basename($targetPath)) ? 'active' : '';
                $isExternal = preg_match('#^https?://#', $file) === 1;
                if ($isExternal) {
                    $href = $file;
                } else {
                    $href = $rootPrefix . ltrim($targetPath, '/');
                    $query = parse_url($file, PHP_URL_QUERY);
                    if (!empty($query)) {
                        $href .= '?' . $query;
                    }
                    $fragment = parse_url($file, PHP_URL_FRAGMENT);
                    if (!empty($fragment)) {
                        $href .= '#' . $fragment;
                    }
                }
                $hrefEsc = htmlspecialchars($href, ENT_QUOTES, 'UTF-8');
                $titleEsc = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
                $iconEsc = htmlspecialchars($icon, ENT_QUOTES, 'UTF-8');
                echo <<<HTML
                <li class="nav-item">
                    <a class="nav-link $active" href="$hrefEsc">
                        <i class="fas $iconEsc me-2"></i> $titleEsc
                    </a>
                </li>
                HTML;
            }
            echo '</fieldset>';
        }
        ?>
        </li>
        <li class="nav-item mt-2 border-top border-secondary pt-2">
            <a class="nav-link text-danger" href="<?php echo htmlspecialchars($rootPrefix . 'logout.php', ENT_QUOTES, 'UTF-8'); ?>">
                <i class="fas fa-sign-out-alt me-2"></i> Logout
            </a>
        </li>
    </ul>
</nav>
