<?php
if (session_status() !== PHP_SESSION_ACTIVE) session_start();
$prefix = (strpos($_SERVER['PHP_SELF'], '/admin/') !== false) ? '../' : '';
$settingsFile = __DIR__ . '/../data/settings.json';
$settings = file_exists($settingsFile) ? json_decode((string)file_get_contents($settingsFile), true) : [];
if (!is_array($settings)) $settings = [];
$showEvents = array_key_exists('events_show_in_nav',$settings) ? !empty($settings['events_show_in_nav']) : true;
$showPlayer = array_key_exists('player_show_in_nav',$settings) ? !empty($settings['player_show_in_nav']) : true;
$showTop50 = array_key_exists('top50_show_in_nav',$settings) ? !empty($settings['top50_show_in_nav']) : true;
$showChat = array_key_exists('chat_show_in_nav',$settings) ? !empty($settings['chat_show_in_nav']) : true;
$showFans = array_key_exists('fans_show_in_nav',$settings) ? !empty($settings['fans_show_in_nav']) : true;
$showTeam = array_key_exists('team_show_in_nav',$settings) ? !empty($settings['team_show_in_nav']) : true;
$pages = [];
$pagesFile = __DIR__ . '/../data/pages.json';
if (file_exists($pagesFile)) {
  $pages = json_decode((string)file_get_contents($pagesFile), true);
  if (!is_array($pages)) $pages = [];
}
$extraNav = [];
$extraFile = __DIR__ . '/../data/nav_links.json';
if (file_exists($extraFile)) {
  $extraNav = json_decode((string)file_get_contents($extraFile), true);
  if (!is_array($extraNav)) $extraNav = [];
}
$isAdmin = !empty($_SESSION['admin_logged_in']) || !empty($_SESSION['user']);
?>
<nav class="navbar navbar-expand-xl navbar-dark thunder-topnav mb-4">
  <div class="container">
    <a class="navbar-brand thunder-brand" href="<?php echo $prefix; ?>index.php">
      <i class="fa-solid fa-bolt"></i>
      <span>Thunder Mountain Radio</span>
    </a>
    <button class="navbar-toggler thunder-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarNav">
      <ul class="navbar-nav ms-auto align-items-xl-center thunder-nav-list">
        <li class="nav-item"><a class="nav-link" href="<?php echo $prefix; ?>index.php"><i class="fa-solid fa-house"></i><span>Home</span></a></li>
        <li class="nav-item"><a class="nav-link" href="<?php echo $prefix; ?>about.php"><i class="fa-solid fa-circle-info"></i><span>About</span></a></li>
        <li class="nav-item"><a class="nav-link" href="<?php echo $prefix; ?>news.php"><i class="fa-solid fa-newspaper"></i><span>News</span></a></li>
        <?php if ($showEvents): ?><li class="nav-item"><a class="nav-link" href="<?php echo $prefix; ?>events.php"><i class="fa-solid fa-calendar-star"></i><span>Events</span></a></li><?php endif; ?>
        <li class="nav-item"><a class="nav-link" href="<?php echo $prefix; ?>schedule.php"><i class="fa-solid fa-calendar-days"></i><span>Schedule</span></a></li>
        <li class="nav-item"><a class="nav-link" href="<?php echo $prefix; ?>requests.php"><i class="fa-solid fa-music"></i><span>Requests</span></a></li>
        <?php if ($showTop50): ?><li class="nav-item"><a class="nav-link" href="<?php echo $prefix; ?>top50.php"><i class="fa-solid fa-trophy"></i><span>Top 50</span></a></li><?php endif; ?>
        <?php if ($showPlayer): ?><li class="nav-item"><a class="nav-link" href="<?php echo $prefix; ?>popout-player.php" target="_blank" rel="noopener"><i class="fa-solid fa-up-right-from-square"></i><span>Player</span></a></li><?php endif; ?>
        <li class="nav-item"><a class="nav-link" href="<?php echo $prefix; ?>djs.php"><i class="fa-solid fa-microphone-lines"></i><span>Our DJs</span></a></li>
        <?php if ($showChat): ?><li class="nav-item"><a class="nav-link" href="<?php echo $prefix; ?>chat.php"><i class="fa-solid fa-comments"></i><span>Chat</span></a></li><?php endif; ?>
        <li class="nav-item"><a class="nav-link" href="<?php echo $prefix; ?>friends.php"><i class="fa-solid fa-handshake"></i><span>Friends</span></a></li>
        <?php if ($showFans): ?><li class="nav-item"><a class="nav-link" href="<?php echo $prefix; ?>fans.php"><i class="fa-solid fa-star"></i><span>Fans</span></a></li><?php endif; ?>
        <li class="nav-item"><a class="nav-link" href="<?php echo $prefix; ?>contact.php"><i class="fa-solid fa-envelope"></i><span>Contact</span></a></li>
        <?php if ($showTeam): ?><li class="nav-item"><a class="nav-link" href="<?php echo $prefix; ?>team.php"><i class="fa-solid fa-user-plus"></i><span>Join</span></a></li><?php endif; ?>

        <?php foreach ($pages as $slug => $p): if (!empty($p['published']) && !empty($p['show_in_nav'])): ?>
          <li class="nav-item"><a class="nav-link" href="<?php echo $prefix; ?>page.php?slug=<?= urlencode($slug) ?>"><i class="fa-solid fa-file-lines"></i><span><?= htmlspecialchars($p['title'] ?? $slug, ENT_QUOTES, 'UTF-8') ?></span></a></li>
        <?php endif; endforeach; ?>

        <?php foreach ($extraNav as $link): if (!empty($link['enabled']) && !empty($link['label']) && !empty($link['url'])): ?>
          <li class="nav-item"><a class="nav-link" href="<?= htmlspecialchars($link['url'], ENT_QUOTES, 'UTF-8') ?>" <?= !empty($link['new_tab']) ? 'target="_blank" rel="noopener"' : '' ?>><i class="fa-solid fa-link"></i><span><?= htmlspecialchars($link['label'], ENT_QUOTES, 'UTF-8') ?></span></a></li>
        <?php endif; endforeach; ?>

        <?php if ($isAdmin): ?>
          <li class="nav-item"><span class="nav-link thunder-welcome"><i class="fa-solid fa-user-wave"></i><span><?= htmlspecialchars($_SESSION['admin_user'] ?? $_SESSION['user'] ?? 'Admin', ENT_QUOTES, 'UTF-8') ?></span></span></li>
          <li class="nav-item nav-item-admin-login"><a class="nav-link thunder-admin-link" href="<?php echo $prefix; ?>admin/index.php"><i class="fa-solid fa-screwdriver-wrench"></i><span>Admin Tools</span></a></li>
        <?php else: ?>
          <li class="nav-item nav-item-admin-login"><a class="nav-link thunder-admin-link" href="<?php echo $prefix; ?>admin/login.php"><i class="fa-solid fa-user-shield"></i><span>Admin Tools</span></a></li>
        <?php endif; ?>
      </ul>
    </div>
  </div>
</nav>

<?php if ($isAdmin && strpos($_SERVER['PHP_SELF'], '/admin/') !== false): ?>
<div class="admin-quickbar-wrap">
  <div class="container">
    <div class="admin-quickbar">
      <a class="admin-quickbar-link" href="<?php echo $prefix; ?>admin/index.php"><i class="fa-solid fa-gauge-high"></i><span>Return to Admin Dashboard</span></a>
      <a class="admin-quickbar-link" href="<?php echo $prefix; ?>index.php"><i class="fa-solid fa-house"></i><span>Return to Main Site</span></a>
      <a class="admin-quickbar-link admin-quickbar-link--warn" href="<?php echo $prefix; ?>admin/logout.php"><i class="fa-solid fa-right-from-bracket"></i><span>Logout</span></a>
    </div>
  </div>
</div>
<?php endif; ?>
