<?php
session_start();
include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/nav.php';
$contactFile = __DIR__ . '/data/contact_messages.txt';
$legacyContactFile = __DIR__ . '/data/contact.txt';
if ((!file_exists($contactFile) || trim((string)@file_get_contents($contactFile)) === '') && file_exists($legacyContactFile) && trim((string)@file_get_contents($legacyContactFile)) !== '') {
  @file_put_contents($contactFile, (string)file_get_contents($legacyContactFile), LOCK_EX);
}
function thunder_parse_contact_blocks(string $file): array {
  if (!file_exists($file) || filesize($file) === 0) return [];
  $raw = trim((string)file_get_contents($file));
  if ($raw === '') return [];
  $out = [];
  foreach (preg_split("/
---\s*
/", $raw) as $block) {
    $block = trim((string)$block);
    if ($block === '') continue;
    $item = ['_raw' => $block];
    if (strpos($block, ':') !== false) {
      foreach (preg_split('/\R/', $block) as $line) {
        if (strpos($line, ':') === false) continue;
        [$k, $v] = array_map('trim', explode(':', $line, 2));
        if ($k !== '') $item[strtolower($k)] = $v;
      }
    } elseif (strpos($block, '|') !== false) {
      $parts = array_map('trim', explode('|', $block, 4));
      $item['timestamp'] = $parts[0] ?? '';
      $item['name'] = $parts[1] ?? '';
      $item['email'] = $parts[2] ?? '';
      $item['message'] = $parts[3] ?? '';
    }
    $out[] = $item;
  }
  return array_reverse($out);
}
$recent = array_slice(thunder_parse_contact_blocks($contactFile), 0, 3);
?>
<main class="content-area">
  <div class="content-wrapper page-shell">
    <section class="content-card center-text">
      <div class="page-intro">
        <h1>📞 Contact Thunder Mountain Radio</h1>
        <p>Got feedback, questions, or just want to say hi? Drop us a message below.</p>
      </div>
      <?php if (!empty($_GET['sent'])): ?><div class="alert alert-success">Your message has been sent.</div><?php endif; ?>
      <?php if (!empty($_GET['err'])): ?><div class="alert alert-danger">Please complete your name, email, and message before sending.</div><?php endif; ?>
      <form method="post" action="send_contact.php" class="form-shell" style="text-align:left;">
        <input type="text" name="name" class="form-control mb-3" placeholder="Your name" required>
        <input type="email" name="email" class="form-control mb-3" placeholder="Your email" required>
        <input type="text" name="subject" class="form-control mb-3" placeholder="Subject">
        <textarea name="message" class="form-control mb-3" placeholder="Your message" rows="5" required></textarea>
        <button type="submit" class="button play-btn">Send Message</button>
      </form>
    </section>
  </div>
  <div class="content-wrapper page-shell">
    <section class="content-card center-text">
      <h2>📬 Recent Messages</h2>
      <div class="page-copy">
      <?php if (empty($recent)): ?>
        <p>No messages yet.</p>
      <?php else: ?>
        <?php foreach ($recent as $entry): ?>
          <div class="comment-box">
            <strong><?= htmlspecialchars((string)($entry['name'] ?? 'Guest')) ?></strong><br>
            <?php if (!empty($entry['email'])): ?><small><?= htmlspecialchars((string)$entry['email']) ?></small><br><?php endif; ?>
            <?php if (!empty($entry['subject'])): ?><em><?= htmlspecialchars((string)$entry['subject']) ?></em><br><?php endif; ?>
            <?= nl2br(htmlspecialchars((string)($entry['message'] ?? ''))) ?>
          </div>
        <?php endforeach; ?>
      <?php endif; ?>
      </div>
    </section>
  </div>
</main>
<?php include __DIR__ . '/includes/footer.php'; ?>
