<?php include 'includes/header.php'; ?>
<?php include 'includes/menu.php'; ?>

<?php
$djsJsonFile = __DIR__ . '/data/djs.json';
$djsTxtFile  = __DIR__ . '/data/djs.txt';
$djs = [];

$defaultImage = '/img/nav-icons/ourdjs.png';
$imageMap = [
  'smoke' => '/assets/djs/_gniting_the_irwaves_.jpg',
  'walfman' => '/assets/djs/_he_one_the_alfman_ack.jpg',
  'wolfman' => '/assets/djs/_he_one_the_alfman_ack.jpg',
  'jennah' => '/assets/djs/_murfette_usic_s_y_orever_.png',
];

function thunder_dj_image(string $name, string $image, array $imageMap, string $defaultImage): string {
  $img = trim($image);
  if ($img !== '') {
    return preg_match('~^https?://~i', $img) ? $img : '/img/djs/' . ltrim($img, '/');
  }
  $lower = strtolower($name);
  foreach ($imageMap as $needle => $path) {
    if (strpos($lower, $needle) !== false) return $path;
  }
  return $defaultImage;
}

if (file_exists($djsJsonFile)) {
  $json = json_decode((string)file_get_contents($djsJsonFile), true);
  if (is_array($json) && !empty($json)) {
    foreach ($json as $row) {
      if (!is_array($row)) continue;
      $name = trim((string)($row['name'] ?? ''));
      $show = trim((string)($row['show'] ?? ''));
      $bio  = trim((string)($row['bio'] ?? ''));
      if ($name === '' && $bio === '') continue;
      $djs[] = [
        'name' => $name,
        'show' => $show,
        'bio' => $bio,
        'image' => thunder_dj_image($name, (string)($row['image'] ?? ''), $imageMap, $defaultImage),
      ];
    }
  }
}

if (empty($djs) && file_exists($djsTxtFile)) {
  $entries = preg_split("/\n---\s*\n/", (string)file_get_contents($djsTxtFile));
  foreach ($entries as $entry) {
    $lines = array_values(array_filter(array_map('trim', preg_split('/\R/', trim($entry))), function($v){ return $v !== ''; }));
    if (count($lines) < 2) continue;
    $name = $lines[0];
    $bio = trim(implode("\n", array_slice($lines, 1)));
    $djs[] = [
      'name' => $name,
      'show' => '',
      'bio' => $bio,
      'image' => thunder_dj_image($name, '', $imageMap, $defaultImage),
    ];
  }
}
?>

<main class="content-area">
  <div class="smurf-divider"></div>
  <div class="content-wrapper">
    <section class="content-card center-text">
      <h1>🎧 Meet the DJs</h1>
      <p>These are the voices behind the thunder — each one bringing their own storm to the airwaves.</p>
    </section>

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

    <div class="friends-grid djs-grid">
      <?php if (!empty($djs)): ?>
        <?php foreach ($djs as $dj): ?>
          <article class="content-card friend-card dj-spotlight-card">
            <div class="friend-top">
              <div class="friend-image dj-image">
                <img src="<?= htmlspecialchars($dj['image']) ?>" alt="<?= htmlspecialchars($dj['name']) ?>">
              </div>
              <div class="friend-meta">
                <div class="top-fan-badge">On Air Talent</div>
                <h3 class="friend-name"><?= htmlspecialchars($dj['name']) ?></h3>
                <?php if (!empty($dj['show'])): ?><p class="dj-showline">Show: <?= htmlspecialchars($dj['show']) ?></p><?php endif; ?>
                <p class="friend-desc"><?= nl2br(htmlspecialchars($dj['bio'])) ?></p>
              </div>
            </div>
          </article>
        <?php endforeach; ?>
      <?php else: ?>
        <section class="content-card center-text">
          <p>No DJs listed yet. The mountain is quiet… for now.</p>
        </section>
      <?php endif; ?>
    </div>
  </div>
  <div class="smurf-divider"></div>
</main>

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