<?php
include __DIR__ . '/includes/header.php';
require_once __DIR__ . '/admin/includes/admin_lib.php';

$file = __DIR__ . '/data/requests.txt';
$statusFile = __DIR__ . '/data/request_status.json';
$items = smurf_parse_blocks($file);
$statusMap = smurf_read_json($statusFile, []);
if (!is_array($statusMap)) $statusMap = [];
function ht_req_key(string $block): string { return sha1($block); }

$companyName = 'Radio Galaxy FX';
$djOptions = [];
$djFile = __DIR__ . '/data/djs.json';
if (is_file($djFile)) {
  $raw = file_get_contents($djFile);
  $decoded = json_decode((string)$raw, true);
  if (is_array($decoded)) {
    foreach ($decoded as $row) {
      if (!is_array($row)) continue;
      $name = trim((string)($row['name'] ?? $row['dj_name'] ?? $row['title'] ?? ''));
      if ($name === '') continue;
      $name = preg_replace('/^[^\p{L}\p{N}]+/u', '', $name);
      $name = preg_replace('/\s+/', ' ', (string)$name);
      if ($name !== '') $djOptions[] = $name;
    }
  }
}
$djOptions = array_values(array_unique(array_filter($djOptions)));
sort($djOptions, SORT_NATURAL | SORT_FLAG_CASE);
if (!$djOptions) {
  $djOptions = ['Station DJ'];
}
?>
<div class="container content-box mt-5">
  <div class="text-center mb-5">
    <h1>Song Requests</h1>
    <p class="subtext">Send a request, dedication, or shoutout to the team.</p>
  </div>

  <?php if (!empty($_GET['sent'])): ?>
    <div class="alert alert-success">Thanks — your request has been sent.</div>
  <?php elseif (!empty($_GET['error'])): ?>
    <div class="alert alert-danger">Please complete the required request fields.</div>
  <?php endif; ?>

  <div class="bg-light p-4 rounded mb-5">
    <form method="POST" action="/send_request.php">
      <input type="hidden" name="company" value="<?php echo htmlspecialchars($companyName); ?>">
      <div class="mb-3">
        <label for="dj_name">Choose DJ</label>
        <select name="dj_name" id="dj_name" class="form-control" required>
          <option value="">Select DJ</option>
          <?php foreach ($djOptions as $dj): ?>
            <option value="<?php echo htmlspecialchars($dj); ?>"><?php echo htmlspecialchars($dj); ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="mb-3">
        <label for="company">Station</label>
        <input type="text" id="company" class="form-control" value="<?php echo htmlspecialchars($companyName); ?>" disabled>
      </div>
      <div class="mb-3">
        <label for="name">Your Name</label>
        <input type="text" name="name" id="name" class="form-control" required>
      </div>
      <div class="mb-3">
        <label for="email">Your Email</label>
        <input type="email" name="email" id="email" class="form-control">
      </div>
      <div class="mb-3">
        <label for="song">Song Title</label>
        <input type="text" name="song" id="song" class="form-control" required>
      </div>
      <div class="mb-3">
        <label for="artist">Artist</label>
        <input type="text" name="artist" id="artist" class="form-control" required>
      </div>
      <div class="mb-3">
        <label for="genre">Genre</label>
        <input type="text" name="genre" id="genre" class="form-control" placeholder="Optional genre">
      </div>
      <div class="mb-3">
        <label for="dedication">Dedication / Message</label>
        <textarea name="dedication" id="dedication" class="form-control" rows="5" placeholder="Optional message or shoutout"></textarea>
      </div>
      <button type="submit" class="btn btn-dark">Send Request</button>
    </form>
  </div>

  <div class="content-box mt-5">
    <h4>Recent Accepted Requests</h4>
    <?php
    $shown = 0;
    foreach ($items as $item) {
      $k = ht_req_key($item['_raw'] ?? '');
      $meta = $statusMap[$k] ?? ['status' => 'Pending'];
      if (($meta['status'] ?? 'Pending') !== 'Accepted' && ($meta['status'] ?? '') !== 'Played') continue;
      $shown++;
      echo '<div class="comment-box">';
      echo '<strong>' . htmlspecialchars((string)($item['Name'] ?? 'Listener')) . '</strong>';
      if (!empty($item['Song'])) echo ' requested <em>' . htmlspecialchars((string)$item['Song']) . '</em>';
      if (!empty($item['Artist'])) echo ' by ' . htmlspecialchars((string)$item['Artist']);
      if (!empty($item['DJ_Name'])) echo '<br><small>For DJ: ' . htmlspecialchars((string)$item['DJ_Name']) . '</small>';
      if (!empty($item['Dedication'])) echo '<br>' . nl2br(htmlspecialchars((string)$item['Dedication']));
      echo '</div>';
      if ($shown >= 5) break;
    }
    if ($shown === 0) echo '<p>No accepted requests yet.</p>';
    ?>
  </div>
</div>
<?php include __DIR__ . '/includes/footer.php'; ?>
