<?php
$settingsFile = __DIR__ . '/../data/settings.json';
$settings = [];
if (file_exists($settingsFile)) {
  $settings = json_decode(file_get_contents($settingsFile), true);
  if (!is_array($settings)) $settings = [];
}
$chatEnabled = !empty($settings['chat_enabled']);
$chatShowNav = array_key_exists('chat_show_in_nav', $settings) ? !empty($settings['chat_show_in_nav']) : true;
$pagesFile = __DIR__ . '/../data/pages.json';
$customPages = [];
if (file_exists($pagesFile)) {
  $customPages = json_decode(file_get_contents($pagesFile), true);
  if (!is_array($customPages)) $customPages = [];
}
$extraNavFile = __DIR__ . '/../data/nav_links.json';
$extraNavLinks = [];
if (file_exists($extraNavFile)) {
  $extraNavLinks = json_decode(file_get_contents($extraNavFile), true);
  if (!is_array($extraNavLinks)) $extraNavLinks = [];
}
require_once __DIR__ . '/schedule_lib.php';

// Schedule-based live DJ detection
$schedule = smurf_load_schedule_hourmap(__DIR__ . '/../data/schedule.json', __DIR__ . '/../data/schedule.txt', $SMURF_DAYS_ORDER);
$nn = smurf_schedule_now_next($schedule);
$currentDJ = $nn['now'] ?: ($settings['default_dj'] ?? 'Auto DJ');
$isLiveDJ = smurf_is_live_dj($currentDJ, $settings);
$requireLive = array_key_exists('chat_require_live', $settings) ? !empty($settings['chat_require_live']) : true;
$showChatLink = ($chatEnabled && $chatShowNav && (!$requireLive || $isLiveDJ));
$eventsShowNav = array_key_exists('events_show_in_nav', $settings) ? !empty($settings['events_show_in_nav']) : true;
$top50Enabled = array_key_exists('top50_enabled', $settings) ? !empty($settings['top50_enabled']) : true;
$top50ShowNav = array_key_exists('top50_show_in_nav', $settings) ? !empty($settings['top50_show_in_nav']) : true;
$playerShowNav = array_key_exists('player_show_in_nav', $settings) ? !empty($settings['player_show_in_nav']) : false;
?>
<!-- Mobile overlay -->
<div id="navOverlay" class="nav-overlay" aria-hidden="true"></div>

<nav id="sideNav" class="side-nav" aria-label="Site navigation">
  <div class="nav-top">
    <div class="nav-title">Menu</div>
    <button class="nav-close" type="button" aria-label="Close menu">✕</button>
  </div>

  <ul class="nav-list">
    <li><a href="/index.php"><img src="/img/nav-icons/home.png" alt=""><span>Home</span></a></li>
    <li><a href="/about.php"><img src="/img/nav-icons/about.png" alt=""><span>About</span></a></li>
    <li><a href="/news.php"><img src="/img/nav-icons/news.png" alt=""><span>News</span></a></li>
    <?php if ($eventsShowNav): ?>
    <li><a href="/events.php"><img src="/img/nav-icons/events.png" alt=""><span>Events</span></a></li>
    <?php endif; ?>
    <li><a href="/schedule.php"><img src="/img/nav-icons/schedule.png" alt=""><span>Schedule</span></a></li>
    <li><a href="/requests.php"><img src="/img/nav-icons/requests.png" alt=""><span>Requests</span></a></li>
    <?php if ($top50Enabled && $top50ShowNav): ?>
    <li><a href="/top50.php"><img src="/img/nav-icons/top50.png" alt=""><span>Top 50</span></a></li>
    <?php endif; ?>
    <?php if ($playerShowNav): ?>
    <li><a href="/popout-player.php" target="_blank" rel="noopener"><img src="/img/nav-icons/player.png" alt=""><span>Pop Out Player</span></a></li>
    <?php endif; ?>
    <li><a href="/djs.php"><img src="/img/nav-icons/ourdjs.png" alt=""><span>Our DJs</span></a></li>
    <?php if ($showChatLink): ?>
    <li><a href="/chat.php"><img src="/img/nav-icons/chat.png" alt=""><span>Live Chat</span></a></li>
    <?php endif; ?>
    <li><a href="/friends.php"><img src="/img/nav-icons/friends.png" alt=""><span>Friends</span></a></li>
    <li><a href="/fans.php"><img src="/img/nav-icons/fans.png" alt=""><span>Fans</span></a></li>
    <li><a href="/contact.php"><img src="/img/nav-icons/contact.png" alt=""><span>Contact</span></a></li>
    <li><a href="/team.php"><img src="/img/nav-icons/join.png" alt=""><span>Join the Team</span></a></li>
    <?php foreach ($customPages as $slug => $p): ?>
      <?php if (!empty($p['published']) && !empty($p['show_in_nav'])): ?>
        <li><a href="/page.php?slug=<?= urlencode($slug) ?>"><img src="/img/nav-icons/page.png" alt=""><span><?= htmlspecialchars($p['title'] ?? $slug, ENT_QUOTES, 'UTF-8') ?></span></a></li>
      <?php endif; ?>
    <?php endforeach; ?>
    <?php foreach ($extraNavLinks as $link): ?>
      <?php if (!empty($link['enabled']) && !empty($link['label']) && !empty($link['url'])): ?>
        <li><a href="<?= htmlspecialchars($link['url'], ENT_QUOTES, 'UTF-8') ?>" <?= !empty($link['new_tab']) ? 'target="_blank" rel="noopener"' : '' ?>><img src="/img/nav-icons/page.png" alt=""><span><?= htmlspecialchars(($link['icon'] ?? '🔗') . ' ' . ($link['label'] ?? ''), ENT_QUOTES, 'UTF-8') ?></span></a></li>
      <?php endif; ?>
    <?php endforeach; ?>

    <li class="nav-admin">
      <a href="/admin/index.php"><img src="/img/nav-icons/admin.png" alt=""><span>Admin</span></a>
    </li>
  </ul>
</nav>