<?php
if (session_status() !== PHP_SESSION_ACTIVE) session_start();
$role = $_SESSION['role'] ?? 'guest';
$username = $_SESSION['username'] ?? null;
$isAdminNav = in_array($role, ['master', 'admin', 'dj'], true);
?>

<nav class="main-menu">
  <div class="main-menu-inner">
    <ul class="main-menu-list">
    <li><a href="/index.php">Home</a></li>
    <li><a href="/schedule/schedule.php">Schedule</a></li>
    <li><a href="/djs/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/index.php">News</a></li>
    <li><a href="/about/index.php">About</a></li>
    <li><a href="/top50/index.php">Top 50</a></li>
    <li><a href="/requests/index.php">Requests</a></li>
    <li><a href="/friends/index.php">Friends</a></li>

    <?php
      $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']);
    ?>
    <?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>Logged in: <?= htmlspecialchars($username) ?></span></li>
      <li class="menu-cta"><a href="/login/logout.php">Logout</a></li>
    <?php endif; ?>
    </ul>
  </div>
</nav>
