<?php
session_start();
include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/sidebar.php';

$fansFile = __DIR__ . '/data/fans.json';
$fans = file_exists($fansFile) ? json_decode(file_get_contents($fansFile), true) : [];
if (!is_array($fans)) $fans = [];
?>
<div class="main-wrap">
  <div class="content-panel">
    <div class="friends-banner">
      <h1 class="friends-banner-text">⭐ Oldies &amp; More Fans</h1>
      <p class="friends-subtext">Supporters and listeners who help keep the jukebox alive.</p>
    </div>
    <div class="vinyl-divider center-divider"></div>
    <?php if (!empty($fans)): ?>
      <div class="friends-grid-2">
        <?php foreach ($fans as $fan): ?>
          <?php
            $name = $fan['name'] ?? '';
            $desc = $fan['description'] ?? '';
            $link = $fan['link'] ?? ($fan['url'] ?? '');
            $img = trim((string)($fan['image'] ?? ''));
            $src = '';
            if ($img !== '') {
              if (preg_match('#^https?://#i', $img) || str_starts_with($img, '/')) $src = $img;
              else $src = 'img/friends/' . ltrim($img, '/');
            }
          ?>
          <div class="neon-card friend-card">
            <?php if ($src): ?>
              <div class="friend-image-wrap">
                <img src="<?= htmlspecialchars($src) ?>" alt="<?= htmlspecialchars($name) ?>" class="friend-image">
              </div>
            <?php endif; ?>
            <h2 class="friend-name"><?= !empty($fan['top_fan']) ? '⭐ ' : '' ?><?= htmlspecialchars($name) ?></h2>
            <p class="friend-desc"><?= nl2br(htmlspecialchars($desc)) ?></p>
            <?php if ($link): ?>
              <a href="<?= htmlspecialchars($link) ?>" target="_blank" class="button radio-static friend-btn">🔗 Visit</a>
            <?php endif; ?>
          </div>
        <?php endforeach; ?>
      </div>
    <?php else: ?>
      <div class="content-box center-text"><p>No fans listed yet.</p></div>
    <?php endif; ?>
    <div class="vinyl-divider center-divider"></div>
    <div class="content-box center-text"><a href="index.php" class="button radio-static">⬅ Back to Home</a></div>
    <?php include __DIR__ . '/includes/footer.php'; ?>
  </div>
</div>
