<?php
if (session_status() !== PHP_SESSION_ACTIVE && !headers_sent()) session_start();
$role = $_SESSION['role'] ?? 'guest';
$username = $_SESSION['username'] ?? null;
$isAdminNav = in_array($role, ['master', 'admin', 'dj'], true);
$ahSettingsFile = __DIR__ . '/data/settings.json';
$ahPagesFile = __DIR__ . '/data/pages.json';
$ahSettings = file_exists($ahSettingsFile) ? json_decode(file_get_contents($ahSettingsFile), true) : [];
if (!is_array($ahSettings)) $ahSettings = [];
$ahPages = file_exists($ahPagesFile) ? json_decode(file_get_contents($ahPagesFile), true) : [];
if (!is_array($ahPages)) $ahPages = [];
$showEvents = array_key_exists('events_show_in_nav', $ahSettings) ? !empty($ahSettings['events_show_in_nav']) : true;
$showChat   = !empty($ahSettings['chat_enabled']) && (array_key_exists('chat_show_in_nav', $ahSettings) ? !empty($ahSettings['chat_show_in_nav']) : true);
$showPlayer = !empty($ahSettings['player_show_in_nav']);
?>
<nav class="main-menu">
  <div class="main-menu-inner shell-wide">
    <ul class="main-menu-list">
      <li><a href="/index.php">Home</a></li>
      <li><a href="/schedule.php">Schedule</a></li>
      <li><a href="/djs.php">Meet the DJs</a></li>
      <li><a href="/comments/index.php">Comments</a></li>
      <li><a href="/poll/index.php">Poll</a></li>
      <li><a href="/news.php">News</a></li>
      <li><a href="/about.php">About</a></li>
      <li><a href="/top50.php">Top 50</a></li>
      <li><a href="/requests.php">Requests</a></li>
      <li><a href="/friends.php">Friends</a></li>
      <?php if ($showEvents): ?><li><a href="/events.php">Events</a></li><?php endif; ?>
      <li><a href="/fans.php">Fans</a></li>
      <li><a href="/contact.php">Contact</a></li>
      <li><a href="/team.php">Join the Team</a></li>
      <?php if ($showChat): ?><li><a href="/chat.php">Live Chat</a></li><?php endif; ?>
      <?php if ($showPlayer): ?><li><a href="/popout-player.php" target="_blank" rel="noopener">Player</a></li><?php endif; ?>
      <?php foreach ($ahPages as $slug => $p): ?>
        <?php if (!empty($p['published']) && !empty($p['show_in_nav'])): ?>
          <li><a href="/page.php?slug=<?= urlencode($slug) ?>"><?= htmlspecialchars($p['title'] ?? $slug) ?></a></li>
        <?php endif; ?>
      <?php endforeach; ?>
      <?php if (!$username): ?>
        <li class="menu-cta"><a href="/login/index.php">Login</a></li>
      <?php else: ?>
        <?php if ($isAdminNav): ?><li class="menu-cta admin-link"><a href="/admin/dashboard.php">Admin Panel</a></li><?php endif; ?>
        <li class="menu-user"><span><?= htmlspecialchars($username) ?></span></li>
        <li class="menu-cta"><a href="/login/logout.php">Logout</a></li>
      <?php endif; ?>
    </ul>
  </div>
</nav>
