<?php
session_start();
$role = $_SESSION['role'] ?? 'guest';
if (!in_array($role, ['master', 'admin'])) {
  header('Location: /login/index.php');
  exit;
}
?>
<?php include('../header.php'); ?>
<?php include('../menu.php'); ?>
<link rel="stylesheet" href="/css/style.css?v=1">

<main class="main-container">
  <section class="admin-card">
    <h2 class="text-warning">🎛️ Manage DJs</h2>
    <p class="text-muted">Logged in as <strong><?= htmlspecialchars($_SESSION['username']) ?></strong> (<?= $role ?>)</p>

    <?php
      $file = '../data/djs.txt';
      $imageDir = '../assets/djs/';
      $djs = [];
      $entries = file_exists($file) ? explode("---\n", file_get_contents($file)) : [];

      foreach ($entries as $index => $entry) {
        $lines = explode("\n", trim($entry));
        if (count($lines) < 2) continue;

        $name = trim($lines[0]);
        $bio = trim($lines[1]);
        $image = $lines[2] ?? '';
        $visibility = $lines[3] ?? 'visible';
        $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 = 'assets/djs/' . $slug . '.' . $ext;
            $serverPath = '../' . $webPath;
            if (file_exists($serverPath)) {
              $imgSrc = $webPath;
              break;
            }
          }
        }

        $djs[] = [
          'id' => $index,
          'name' => $name,
          'bio' => $bio,
          'image' => $image,
          'visibility' => $visibility,
          'slug' => $slug,
          'imgSrc' => $imgSrc
        ];
      }
    ?>

    <form method="POST" enctype="multipart/form-data" action="save_djs.php" class="admin-form">
      <input type="hidden" name="return_to" value="<?= htmlspecialchars($_SERVER['REQUEST_URI'] ?? '/admin/djs.php') ?>">
      <h3 class="text-warning">➕ Add / Edit DJ</h3>

      <?php if (!empty($djs)): ?>
        <?php foreach ($djs as $dj): ?>
          <div class="admin-dj-card mb-4">
            <h4><?= htmlspecialchars($dj['name']) ?></h4>
            <?php if ($dj['imgSrc']): ?>
              <img src="<?= $dj['imgSrc'] ?>" alt="DJ Image" class="dj-photo mb-2">
            <?php endif; ?>
            <textarea name="bio_<?= $dj['id'] ?>" class="admin-textarea mb-2" rows="4"><?= htmlspecialchars($dj['bio']) ?></textarea>
            <label class="form-label">Replace Image</label>
            <input type="file" name="image_<?= $dj['id'] ?>" class="admin-input mb-2">
            <label class="form-label">Or Image URL</label>
            <input type="url" name="image_url_<?= $dj['id'] ?>" class="admin-input mb-2" value="<?= htmlspecialchars($dj['image']) ?>">
            <label class="form-label">Visibility</label>
            <select name="visibility_<?= $dj['id'] ?>" class="admin-select mb-2">
              <option value="visible" <?= $dj['visibility'] === 'visible' ? 'selected' : '' ?>>Visible</option>
              <option value="hidden" <?= $dj['visibility'] === 'hidden' ? 'selected' : '' ?>>Hidden</option>
            </select>
            <input type="hidden" name="name_<?= $dj['id'] ?>" value="<?= htmlspecialchars($dj['name']) ?>">
            <button type="submit" name="save_<?= $dj['id'] ?>" class="btn btn-warning btn-sm">💾 Save Changes</button>
            <a href="delete_dj.php?delete=<?= $dj['id'] ?>&return_to=<?= urlencode($_SERVER['REQUEST_URI'] ?? '/admin/djs.php') ?>" class="btn btn-danger btn-sm">🗑️ Delete</a>
          </div>
        <?php endforeach; ?>
      <?php else: ?>
        <div class="admin-dj-card mb-4">
          <p>No DJs found. Use the form below to add the first voice to the mountain.</p>
        </div>
      <?php endif; ?>
    </form>

    <form method="POST" enctype="multipart/form-data" action="save_djs.php" class="admin-form mt-5">
      <input type="hidden" name="return_to" value="<?= htmlspecialchars($_SERVER['REQUEST_URI'] ?? '/admin/djs.php') ?>">
      <h3 class="text-warning">➕ Add New DJ</h3>
      <input type="text" name="new_name" class="admin-input mb-2" placeholder="DJ name" required>
      <textarea name="new_bio" class="admin-textarea mb-2" rows="4" placeholder="DJ bio" required></textarea>
      <label class="form-label">Upload Image</label>
      <input type="file" name="new_image" class="admin-input mb-2">
      <label class="form-label">Or Image URL</label>
      <input type="url" name="new_image_url" class="admin-input mb-2">
      <label class="form-label">Visibility</label>
      <select name="new_visibility" class="admin-select mb-2">
        <option value="visible">Visible</option>
        <option value="hidden">Hidden</option>
      </select>
      <button type="submit" name="add_new" class="btn btn-dark">➕ Add DJ</button>
    </form>
  </section>

  <div class="text-end mt-4">
    <a href="/dj/djs.php" class="btn btn-sm btn-dark">View Public DJ Page</a>
  </div>
</main>

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