<?php include('../header.php'); ?>
<?php include('../menu.php'); ?>
<link rel="stylesheet" href="/css/style.css?v=1">

<main class="main-container">
  <section class="schedule-preview">
    <h2 class="text-warning">🎧 Meet the DJs</h2>
    <p class="mb-4">These are the voices behind the thunder — each one bringing their own storm to the airwaves.</p>

    <?php
      $djsFile = '../data/djs.txt';
      $imageDir = '/assets/djs/';
      $djs = [];

      if (file_exists($djsFile)) {
        $entries = explode("---\n", file_get_contents($djsFile));
        foreach ($entries as $entry) {
          $lines = array_map('trim', explode("\n", trim($entry)));
          if (count($lines) < 4) continue;

          [$name, $bio, $image, $visibility] = $lines;
          if (strtolower($visibility) !== 'visible') continue;

          $slug = strtolower(preg_replace('/[^a-z0-9]+/', '_', $name));
          $imgSrc = '';

          if (filter_var($image, FILTER_VALIDATE_URL)) {
            $imgSrc = $image;
          } else {
            foreach (['jpg', 'jpeg', 'png', 'webp', 'gif'] as $ext) {
              $webPath = $imageDir . $slug . '.' . $ext;
              $serverPath = $_SERVER['DOCUMENT_ROOT'] . $webPath;
              if (file_exists($serverPath)) {
                $imgSrc = $webPath;
                break;
              }
            }
          }

          $djs[] = ['name' => $name, 'bio' => $bio, 'image' => $imgSrc];
        }
      }
    ?>

    <div class="friends-grid">
      <?php if (!empty($djs)): ?>
        <?php foreach ($djs as $index => $dj): ?>
          <div class="friend-card">
            <?php if ($dj['image']): ?>
              <img src="<?= htmlspecialchars($dj['image']) ?>" 
                   alt="<?= htmlspecialchars($dj['name']) ?>" 
                   class="friend-photo mb-2">
            <?php endif; ?>
            <h4><?= htmlspecialchars($dj['name']) ?></h4>
            <p><?= nl2br(htmlspecialchars($dj['bio'])) ?></p>
          </div>
          <?php if ($index < count($djs) - 1): ?>
            <hr class="friend-divider">
          <?php endif; ?>
        <?php endforeach; ?>
      <?php else: ?>
        <div class="friend-card">
          <p>No DJs listed yet. The mountain is quiet… for now.</p>
        </div>
      <?php endif; ?>
    </div>
  </section>
</main>

<?php include('../footer.php'); ?>
