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

$sent = isset($_GET['sent']) && $_GET['sent'] == '1';

// Recently played requests (from admin workflow)
$requestsFile = __DIR__ . '/data/requests.txt';
$statusFile = __DIR__ . '/data/request_status.json';
$statusMap = file_exists($statusFile) ? json_decode(file_get_contents($statusFile), true) : [];
if (!is_array($statusMap)) $statusMap = [];

function smurf_blocks(string $file): array {
  if (!file_exists($file) || filesize($file)===0) return [];
  $raw = file_get_contents($file);
  $parts = preg_split("/\n---\s*\n/", $raw);
  $out = [];
  foreach ($parts as $p) { $p = trim($p); if ($p!=='') $out[] = $p; }
  return array_reverse($out);
}
function smurf_req_key_front(string $block): string { return sha1($block); }

$played = [];
foreach (smurf_blocks($requestsFile) as $block) {
  $k = smurf_req_key_front($block);
  $meta = $statusMap[$k] ?? null;
  if ($meta && (($meta['status'] ?? '') === 'Played')) {
    $played[] = ['block'=>$block,'meta'=>$meta];
    if (count($played) >= 5) break;
  }
}

?>

<main class="content-area">

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

  <div class="content-wrapper">

    <section class="content-card center-text">
      <h1>Request a Song</h1>
      <p>Send a request to Radio Galaxy FX.</p>
    </section>

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

    
    <?php if ($sent): ?>
      <section class="content-card center-text">
        <h2 style="color:#a6ff9e;">Your request has been received! 🎉</h2>
        <p>Thank you — we’ve received it.</p>
      </section>
      <div class="smurf-divider"></div>
    <?php endif; ?>
    

    <?php if (!empty($played)): ?>
  <section class="content-card">
    <h2 style="margin-top:0;">✅ Recently Played Requests</h2>
    <div class="friends-grid" style="grid-template-columns:repeat(auto-fit,minmax(260px,1fr));">
      <?php foreach ($played as $p): ?>
        <?php
          $lines = explode("\n", $p['block']);
          $kv = [];
          foreach ($lines as $ln) {
            if (strpos($ln, ':') !== false) { [$k,$v] = array_map('trim', explode(':', $ln, 2)); $kv[strtolower($k)] = $v; }
          }
          $song = $kv['song'] ?? '';
          $artist = $kv['artist'] ?? '';
          $name = $kv['name'] ?? '';
          $note = $p['meta']['note'] ?? '';
        ?>
        <div class="content-card" style="text-align:left;">
          <strong><?= htmlspecialchars($song ?: 'Request') ?></strong>
          <?php if ($artist): ?><div style="opacity:.9;margin-top:4px;">by <?= htmlspecialchars($artist) ?></div><?php endif; ?>
          <?php if ($name): ?><div style="opacity:.75;margin-top:6px;">Requested by <?= htmlspecialchars($name) ?></div><?php endif; ?>
          <?php if ($note): ?><div style="margin-top:10px;padding:10px;border:1px solid rgba(255,215,120,.35);border-radius:10px;opacity:.95;">🎧 <?= htmlspecialchars($note) ?></div><?php endif; ?>
        </div>
      <?php endforeach; ?>
    </div>
  </section>
  <div class="smurf-divider"></div>
<?php endif; ?>

    <section class="content-card">
      <div class="request-wrapper">
        <form method="post" action="send_request.php">
<div class="request-box">
                <div class="request-field">
                    <label>Your Name</label>
                    <input type="text" class="request-input" name="name" required>
                </div>

                <div class="request-field">
                    <label>Email</label>
                    <input type="email" class="request-input" name="email" required>
                </div>
            </div>

            <div class="request-box">
                <div class="request-field">
                    <label>Song Title</label>
                    <input type="text" class="request-input" name="song" required>
                </div>

                <div class="request-field">
                    <label>Artist</label>
                    <input type="text" class="request-input" name="artist" required>
                </div>
            </div>

            <div class="request-box">
                <div class="request-field">
                    <label>Dedication (optional)</label>
                    <textarea class="request-textarea" name="dedication"></textarea>
                </div>
            </div>

            <div class="request-box center-text">
                <button class="request-button">Send Request 💌</button>
            </div>
        </form>
      </div>
    </section>

  </div>

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

</main>

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