
<?php
session_start();
include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/nav.php';
require_once __DIR__ . '/includes/stream_lib.php';
$settingsFile = __DIR__ . '/data/settings.json';
$settings = file_exists($settingsFile) ? json_decode((string)file_get_contents($settingsFile), true) : [];
if (!is_array($settings)) $settings = [];
if (array_key_exists('top50_enabled', $settings) && empty($settings['top50_enabled'])) {
  http_response_code(404);
  echo '<main class="content-area"><div class="content-wrapper"><section class="content-card center-text"><h1>Top 50 is currently unavailable</h1></section></div></main>';
  include __DIR__ . '/includes/footer.php';
  exit;
}
$current = smurf_top50_touch(__DIR__ . '/data/top50.json', __DIR__ . '/data/top50_state.json');
$rows = smurf_top50_sorted(__DIR__ . '/data/top50.json');
?>
<main class="content-area">
  <div class="content-wrapper">
    <section class="content-card center-text">
      <h1 class="hero-title">🎵 Radio Galaxy FX Top 50</h1>
      <p class="hero-tagline">Auto-built from the station stream history.</p>
      <?php if ($current): ?><p class="top50-now">Now tracking: <strong><?= htmlspecialchars($current) ?></strong></p><?php endif; ?>
    </section>
  </div>
  <div class="smurf-divider"></div>
  <div class="content-wrapper">
    <section class="content-card">
      <?php if (empty($rows)): ?>
        <p class="center-text">No tracks captured yet. Visit the homepage while the stream is playing and the chart will begin to build.</p>
      <?php else: ?>
        <div class="top50-list">
          <?php foreach ($rows as $idx => $row): ?>
            <article class="top50-row <?= $idx < 3 ? 'is-top' : '' ?>">
              <div class="top50-rank">#<?= $idx + 1 ?></div>
              <div class="top50-copy">
                <h3><?= htmlspecialchars($row['title'] ?? '') ?></h3>
                <p><strong>Spins tracked:</strong> <?= (int)($row['count'] ?? 0) ?></p>
                <?php if (!empty($row['last_seen'])): ?><p><strong>Last seen:</strong> <?= htmlspecialchars($row['last_seen']) ?></p><?php endif; ?>
              </div>
            </article>
          <?php endforeach; ?>
        </div>
      <?php endif; ?>
    </section>
  </div>
</main>
<?php include __DIR__ . '/includes/footer.php'; ?>
