<?php
session_start();
include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/nav.php';
$promoFile = __DIR__ . '/data/promo.json';
$promo = file_exists($promoFile) ? json_decode((string)file_get_contents($promoFile), true) : [];
if (!is_array($promo)) $promo = [];
$promo = array_merge([
  'enabled'=>false,'title'=>'','start'=>'','end'=>'','description'=>'','prizes'=>[],
  'image'=>'','show_entry_form'=>true,'show_external_link'=>false,'external_url'=>'','external_label'=>'Open External Events Site','pinned'=>false
], $promo);
$sent = !empty($_GET['sent']);
$error = $_GET['err'] ?? '';
$image = trim((string)($promo['image'] ?? ''));
if ($image !== '' && !preg_match('~^(https?:)?//~i', $image) && strpos($image, '/img/') !== 0) $image = 'img/content/' . ltrim($image, '/');
?>
<main class="content-area">
  <div class="content-wrapper page-shell">
    <section class="content-card center-text">
      <div class="page-intro">
        <h1>🎉 Events & Promotions</h1>
      </div>
      <?php if (empty($promo['enabled'])): ?>
        <p>No promotions running right now. Check back soon.</p>
      <?php else: ?>
        <h3><?= htmlspecialchars($promo['title'] ?: 'Promotion') ?><?= !empty($promo['pinned']) ? ' 📌' : '' ?></h3>
        <?php if ($image !== ''): ?><p><img src="<?= htmlspecialchars($image) ?>" alt="Event image" style="max-width:260px;width:100%;height:auto;border-radius:12px;"></p><?php endif; ?>
        <?php if (!empty($promo['start']) || !empty($promo['end'])): ?><p><strong>Dates:</strong> <?= htmlspecialchars((string)$promo['start']) ?><?= !empty($promo['end']) ? ' to ' . htmlspecialchars((string)$promo['end']) : '' ?></p><?php endif; ?>
        <?php if (!empty($promo['description'])): ?><p><?= nl2br(htmlspecialchars((string)$promo['description'])) ?></p><?php endif; ?>
        <?php if (!empty($promo['prizes']) && is_array($promo['prizes'])): ?><h4>🏆 Prizes</h4><ul><?php foreach ($promo['prizes'] as $prize): ?><li><?= htmlspecialchars((string)$prize) ?></li><?php endforeach; ?></ul><?php endif; ?>
        <?php if (!empty($promo['show_external_link']) && !empty($promo['external_url'])): ?><p><a href="<?= htmlspecialchars((string)$promo['external_url']) ?>" target="_blank" rel="noopener" class="button play-btn"><?= htmlspecialchars((string)($promo['external_label'] ?: 'Open External Events Site')) ?></a></p><?php endif; ?>
        <?php if (!empty($promo['show_entry_form'])): ?>
          <hr>
          <h4>🎟️ Enter Now</h4>
          <?php if ($sent): ?><div class="alert alert-success">Entry received.</div><?php elseif ($error): ?><div class="alert alert-danger"><?= htmlspecialchars($error) ?></div><?php endif; ?>
          <form method="post" action="send_promo_entry.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>
            <textarea name="message" class="form-control mb-3" placeholder="Tell us why you should win"></textarea>
            <button type="submit" class="button play-btn">Submit Entry</button>
          </form>
        <?php endif; ?>
      <?php endif; ?>
    </section>
  </div>
</main>
<?php include __DIR__ . '/includes/footer.php'; ?>
