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

// Load Friends data
$fansFile = __DIR__ . '/data/fans.json';
$fans = file_exists($fansFile) ? json_decode(file_get_contents($fansFile), true) : [];
if (!is_array($fans)) $fans = [];
?>

<main class="content-area">

  <div class="smurf-divider"></div>

  <div class="content-wrapper">

    <section class="content-card center-text">
      <h1>🤝 Almost Heaven Fans & Supporters 🤝</h1>
      <h2>Supporting Almost Heaven Radio</h2>
    </section>

    <div class="friends-grid">
      <?php if (!empty($fans)): ?>
        <?php foreach ($fans as $fan): ?>
          <?php
            $name = $fan['name'] ?? '';
            $desc = $fan['description'] ?? '';
            $link = $fan['link'] ?? ($fan['url'] ?? '');
            $img  = $fan['image'] ?? '';

            $src = '';
            if (!empty($img)) {
              if (preg_match('#^https?://#i', $img)) $src = $img;
              else $src = '/img/friends/' . ltrim($img, '/');
            }
          ?>
          <div class="content-card friend-card fan-card <?= !empty($fan['top_fan']) ? 'top-fan' : '' ?>">

            <div class="friend-top">
              <div class="friend-image">
                <?php if ($src): ?>
                  <img src="<?= htmlspecialchars($src) ?>" alt="<?= htmlspecialchars($name) ?>">
                <?php else: ?>
                  <div class="friend-image-fallback">🤝</div>
                <?php endif; ?>
              </div>

              <div class="friend-meta">
                <?php if (!empty($fan['top_fan'])): ?><div class="top-fan-badge">⭐ Top Fan</div><?php endif; ?>
                <h3 class="friend-name"><?= !empty($fan['top_fan']) ? "⭐ " : "" ?><?= htmlspecialchars($name) ?></h3>
                <?php if (!empty($desc)): ?>
                  <p class="friend-desc"><?= nl2br(htmlspecialchars($desc)) ?></p>
                <?php endif; ?>
              </div>
            </div>

            <?php if (!empty($link)): ?>
              <div class="friend-actions">
                <a class="friend-btn" href="<?= htmlspecialchars($link) ?>" target="_blank" rel="noopener">
                  Visit Website
                </a>
              </div>
            <?php endif; ?>

          </div>
        <?php endforeach; ?>
      <?php else: ?>
        <div class="content-card center-text">
          <p>No fans found.</p>
        </div>
      <?php endif; ?>
    </div>

  </div>

  <div class="smurf-divider"></div>

</main>

<?php include __DIR__ . '/includes/footer.php'; ?>
