<?php
require __DIR__ . '/config.php';
$settings = get_settings();
$success = '';
$error = '';

if (empty($settings['answer_page_open'])) {
    http_response_code(403);
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($settings['answer_page_open'])) {
    $artist = trim($_POST['artist'] ?? '');
    $song = trim($_POST['song'] ?? '');
    $name = trim($_POST['name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $platform = trim($_POST['platform'] ?? '');

    if ($artist === '' || $song === '' || $email === '' || $platform === '') {
        $error = 'Please complete all required fields.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Please enter a valid email address.';
    } else {
        $submissions = get_submissions();
        $submissions[] = [
            'artist' => $artist,
            'song' => $song,
            'name' => $name,
            'email' => $email,
            'platform' => $platform,
            'ip' => $_SERVER['REMOTE_ADDR'] ?? '',
            'timestamp' => date('Y-m-d H:i:s')
        ];
        save_submissions($submissions);
        $success = $settings['success_message'] ?? 'Thanks. Your answer has been received.';
    }
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?= e($settings['form_title']) ?></title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Oswald:wght@400;600;700&family=Inter:wght@400;600;700&display=swap" rel="stylesheet">
<style>
body {
  margin: 0;
  font-family: Inter, sans-serif;
  color: #fff7e7;
  background: linear-gradient(180deg, rgba(15, 5, 2, .7), rgba(15, 5, 2, .94)), url('<?= e($settings['poster']) ?>') center/cover fixed;
}
.wrap { max-width: 720px; margin: 0 auto; padding: 32px 18px 60px; }
.panel {
  background: rgba(20, 8, 4, .84);
  border: 1px solid rgba(255, 202, 90, .22);
  border-radius: 28px;
  padding: 28px;
  box-shadow: 0 24px 60px rgba(0,0,0,.45);
}
h1,h2 { font-family: Oswald, sans-serif; margin: 0 0 12px; }
.label { display:block; margin: 14px 0 6px; font-weight: 700; }
input, select, textarea {
  width: 100%;
  padding: 13px 14px;
  border-radius: 14px;
  border: 1px solid rgba(255,255,255,.12);
  background: rgba(255,255,255,.08);
  color: white;
}
option { color: #111; }
button {
  margin-top: 18px;
  width: 100%;
  border: 0;
  border-radius: 999px;
  padding: 15px 18px;
  background: linear-gradient(180deg, #ffd25a, #ffb300);
  color: #2b1700;
  font-weight: 800;
  cursor: pointer;
}
.alert {
  margin: 16px 0;
  padding: 14px 16px;
  border-radius: 14px;
}
.success { background: rgba(36, 121, 24, .28); border: 1px solid rgba(126, 217, 87, .3); }
.error { background: rgba(135, 18, 18, .28); border: 1px solid rgba(255, 123, 123, .3); }
.back { display:inline-block; margin-bottom: 18px; color:#ffd25a; }
.helper { opacity: .85; }
</style>
</head>
<body>
<div class="wrap">
  <a class="back" href="<?= e(site_url('index.php')) ?>">← Back to event page</a>
  <div class="panel">
    <h1><?= e($settings['form_title']) ?></h1>
    <p class="helper"><?= e($settings['event_name']) ?> — Current Pot: $<?= e((string) $settings['pot']) ?></p>

    <?php if (empty($settings['answer_page_open'])): ?>
      <div class="alert error">The answer page is currently closed. Please check back when the event opens.</div>
    <?php else: ?>
      <?php if ($success): ?><div class="alert success"><?= e($success) ?></div><?php endif; ?>
      <?php if ($error): ?><div class="alert error"><?= e($error) ?></div><?php endif; ?>

      <form method="post">
        <label class="label">Your Name</label>
        <input type="text" name="name" placeholder="Optional display name">

        <label class="label">Artist</label>
        <input type="text" name="artist" required>

        <label class="label">Song</label>
        <input type="text" name="song" required>

        <label class="label">Email</label>
        <input type="email" name="email" required>

        <label class="label">Platform</label>
        <select name="platform" required>
          <option value="">Select platform</option>
          <?php foreach (($settings['platforms'] ?? []) as $platform): ?>
            <option value="<?= e($platform) ?>"><?= e($platform) ?></option>
          <?php endforeach; ?>
        </select>

        <button type="submit">Submit Answer</button>
      </form>
    <?php endif; ?>
  </div>
</div>
</body>
</html>
