
<?php
if (!isset($_SESSION)) session_start();
$nav_items = include __DIR__ . '/../config/navigation.php';
$settingsPath = __DIR__ . '/../data/settings.json';
$settings = file_exists($settingsPath) ? json_decode((string)file_get_contents($settingsPath), true) : [];
if (!is_array($settings)) $settings = [];
$extraPath = __DIR__ . '/../data/nav_links.json';
$extraLinks = file_exists($extraPath) ? json_decode((string)file_get_contents($extraPath), true) : [];
if (!is_array($extraLinks)) $extraLinks = [];
$user_role = (isset($_SESSION['user']) && !empty($_SESSION['user'])) ? 'admin' : 'public';
$hiddenBySetting = [
  'Events' => empty($settings['events_show_in_nav']),
  'Fans' => empty($settings['fans_show_in_nav']),
  'Join the Team' => empty($settings['team_show_in_nav']),
  'Top 50' => array_key_exists('top50_show_in_nav',$settings) ? empty($settings['top50_show_in_nav']) : false,
  'Popout Player' => empty($settings['player_show_in_nav']),
  'Live Chat' => empty($settings['chat_show_in_nav']) || empty($settings['chat_enabled']),
];
usort($nav_items, fn($a,$b) => ($a['order'] ?? 0) <=> ($b['order'] ?? 0));
?>
<div class="sidebar"><div class="sidebar-inner">
<?php foreach ($nav_items as $item): ?>
  <?php
    if (empty($item['visible'])) continue;
    if (!in_array($user_role, $item['roles'] ?? ['public'], true)) continue;
    if (!empty($hiddenBySetting[$item['label'] ?? ''])) continue;
    if ($user_role === 'admin' && ($item['label'] ?? '') === 'Admin') $item['url'] = '/admin/index.php';
  ?>
  <?php if (!empty($item['divider_before'])): ?><div class="sidebar-divider"></div><?php endif; ?>
  <a href="<?= htmlspecialchars($item['url'], ENT_QUOTES, 'UTF-8') ?>" class="sidebar-item <?= htmlspecialchars((string)($item['custom_class'] ?? ''), ENT_QUOTES, 'UTF-8') ?>" data-neon="<?= htmlspecialchars((string)($item['neon_color'] ?? ''), ENT_QUOTES, 'UTF-8') ?>" <?= !empty($item['new_tab']) ? 'target="_blank" rel="noopener"' : '' ?>>
    <span class="sidebar-icon icon-<?= htmlspecialchars((string)($item['icon'] ?? 'home'), ENT_QUOTES, 'UTF-8') ?>"></span>
    <span class="sidebar-label"><?= htmlspecialchars((string)($item['label'] ?? ''), ENT_QUOTES, 'UTF-8') ?></span>
    <?php if (!empty($item['badge'])): ?><span class="sidebar-badge" style="background-color: <?= htmlspecialchars((string)($item['badge_color'] ?? '#4ff'), ENT_QUOTES, 'UTF-8') ?>;"><?= htmlspecialchars((string)$item['badge'], ENT_QUOTES, 'UTF-8') ?></span><?php endif; ?>
    <span class="music-notes"><span class="note n1">♪</span><span class="note n2">♫</span><span class="note n3">♬</span></span>
  </a>
  <?php if (!empty($item['divider_after'])): ?><div class="sidebar-divider"></div><?php endif; ?>
<?php endforeach; ?>
<?php if (!empty($extraLinks)): ?><div class="sidebar-divider"></div><?php endif; ?>
<?php foreach ($extraLinks as $link): ?>
  <?php if (empty($link['enabled']) || empty($link['label']) || empty($link['url'])) continue; ?>
  <a href="<?= htmlspecialchars($link['url'], ENT_QUOTES, 'UTF-8') ?>" class="sidebar-item" <?= !empty($link['new_tab']) ? 'target="_blank" rel="noopener"' : '' ?>>
    <span class="sidebar-icon"><?= htmlspecialchars((string)($link['icon'] ?? '🔗'), ENT_QUOTES, 'UTF-8') ?></span>
    <span class="sidebar-label"><?= htmlspecialchars((string)$link['label'], ENT_QUOTES, 'UTF-8') ?></span>
  </a>
<?php endforeach; ?>
</div></div>
