<?php
$pageTitle = 'Requests — Almost Heaven Radio';
include __DIR__ . '/header.php';
include __DIR__ . '/menu.php';

$sent = isset($_GET['sent']) && $_GET['sent'] === '1';
$err = isset($_GET['err']) ? (string)$_GET['err'] : '';
$requestsFile = __DIR__ . '/data/requests.txt';
$statusFile = __DIR__ . '/data/request_status.json';
$statusMap = file_exists($statusFile) ? json_decode((string)file_get_contents($statusFile), true) : [];
if (!is_array($statusMap)) $statusMap = [];

function ah_request_stable_key(string $raw): string {
  $raw = str_replace(["\r\n", "\r"], "\n", trim($raw));
  $lines = preg_split("/\n+/", $raw) ?: [];
  $keep = [];
  foreach ($lines as $line) {
    if (preg_match('/^Approved\s*:/i', $line)) continue;
    $keep[] = trim($line);
  }
  return md5(implode("\n", $keep));
}

function ah_parse_requests_public(string $file): array {
  if (!file_exists($file) || trim((string)@file_get_contents($file)) === '') return [];
  $raw = trim((string)@file_get_contents($file));
  $items = [];
  if (strpos($raw, '---') !== false) {
    foreach (preg_split('/\R---\s*\R/', $raw) as $block) {
      $block = trim((string)$block);
      if ($block === '') continue;
      $item = ['_raw' => $block];
      foreach (preg_split('/\R+/', $block) as $line) {
        if (preg_match('/^([^:]+):\s*(.*)$/', $line, $m)) $item[strtolower(trim($m[1]))] = trim($m[2]);
      }
      $item['_key'] = ah_request_stable_key($block);
      $items[] = $item;
    }
  }
  return array_reverse($items);
}

$items = ah_parse_requests_public($requestsFile);
?>
<main class="content-area"><div class="content-wrapper page-shell"><section class="content-card"><h1>🎶 Song Requests</h1><p>Send a request to the station and see approved requests below.</p><?php if($sent): ?><div class="comment-item">Request sent successfully.</div><?php elseif($err !== ''): ?><div class="comment-item">Please complete all required request fields.</div><?php endif; ?><form method="post" action="/send_request.php" class="d-grid" style="gap:14px;margin-top:18px;"><div class="row g-3"><div class="col-md-6"><input type="text" name="name" placeholder="Your name" required></div><div class="col-md-6"><input type="email" name="email" placeholder="Your email" required></div></div><div class="row g-3"><div class="col-md-6"><input type="text" name="song" placeholder="Song title" required></div><div class="col-md-6"><input type="text" name="artist" placeholder="Artist" required></div></div><textarea name="dedication" rows="5" placeholder="Dedication or note"></textarea><div><button class="play-button" type="submit">Send Request</button></div></form></section><section class="content-card"><h2>Approved Requests</h2><div class="table-like-list"><?php $shown=0; foreach($items as $item): $approved=(string)($item['approved'] ?? '0'); $key=(string)($item['_key'] ?? ''); $state=strtolower((string)($statusMap[$key]['status'] ?? '')); $publicShow=($state==='approved' || $state==='played' || strtolower($approved)==='1'); if(!$publicShow) continue; $shown++; ?><li><strong><?= htmlspecialchars((string)($item['song'] ?? 'Song')) ?> — <?= htmlspecialchars((string)($item['artist'] ?? 'Artist')) ?></strong><br><span>Requested by <?= htmlspecialchars((string)($item['name'] ?? 'Listener')) ?></span><?php if(!empty($item['dedication'])): ?><br><span><?= htmlspecialchars((string)$item['dedication']) ?></span><?php endif; ?><?php if(!empty($statusMap[$key]['note'])): ?><br><span><?= htmlspecialchars((string)$statusMap[$key]['note']) ?></span><?php endif; ?></li><?php endforeach; if(!$shown): ?><li>No approved requests are live yet.</li><?php endif; ?></div></section></div></main>
<?php include __DIR__ . '/footer.php'; ?>
