<?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 = [];
}
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));
?>
<!-- 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>
    <li><a href="/events.php"><img src="/img/nav-icons/events.png" alt=""><span>Events</span></a></li>
    <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>
    <li><a href="/top50.php"><img src="/img/nav-icons/top50.png" alt=""><span>Top 50</span></a></li>
    <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; ?>


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