<?php require_once __DIR__ . '/../config.php'; require_admin(); $settings = get_settings(); ?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Admin Control Room</title>
<link rel="stylesheet" href="../assets/css/style.css">
</head>
<body>
<div class="admin-shell">
  <aside class="admin-sidebar">
    <div class="brand">
      <img src="../assets/site-logo.png" alt="Logo">
      <div>
        <h2>Control Room</h2>
        <div class="meta">Broadcasturself Admin</div>
      </div>
    </div>
    <nav>
      <a href="index.php">Dashboard</a>
      <a href="site-control.php">Site Control</a>
      <a href="stations.php">Stations</a>
      <a href="schedule.php">Schedule</a>
      <a href="clues.php">Clues</a>
      <a href="answers.php">Answers</a>
      <a href="submissions.php">Submissions</a>
      <a href="dj-control.php">DJ Control</a>
      <a href="network.php">Network Sync</a>
      <a href="listeners.php">Listener Panel</a>
      <a href="poll.php">Poll Control</a>
      <a href="logout.php">Logout</a>
    </nav>
  </aside>
  <main class="admin-main">
    <?php
    $answers = get_answers();
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
      if (isset($_POST['save_answers'])) {
        $labels = $_POST['label'] ?? [];
        $answerValues = $_POST['answer'] ?? [];
        $variantsValues = $_POST['variants'] ?? [];
        $activeIndex = isset($_POST['active_index']) ? (int) $_POST['active_index'] : 0;
        $newAnswers = [];
        foreach ($labels as $i => $label) {
          $variantLines = preg_split('/\r\n|\r|\n/', (string) ($variantsValues[$i] ?? ''));
          $variantLines = array_values(array_filter(array_map('trim', $variantLines), fn($v) => $v !== ''));
          $newAnswers[] = [
            'id' => $i + 1,
            'label' => (string) $label,
            'answer' => (string) ($answerValues[$i] ?? ''),
            'variants' => $variantLines,
            'active' => $i === $activeIndex,
          ];
        }
        save_answers($newAnswers);
        $answers = $newAnswers;
      }
    }
    ?>
    <div class="card">
      <h1>Answers</h1>
      <p class="meta">Choose one active answer. The submit form will use the active answer for auto winner detection. Add alternate accepted variants one per line.</p>
      <form method="post">
        <?php foreach ($answers as $i => $answer): ?>
          <div class="panel" style="margin-bottom:16px">
            <label><input type="radio" name="active_index" value="<?= $i ?>" <?= !empty($answer['active']) ? 'checked' : '' ?>> Active answer</label>
            <label>Label</label>
            <input type="text" name="label[]" value="<?= esc((string) ($answer['label'] ?? '')) ?>">
            <label>Correct answer</label>
            <input type="text" name="answer[]" value="<?= esc((string) ($answer['answer'] ?? '')) ?>">
            <label>Accepted variants (one per line)</label>
            <textarea name="variants[]"><?= esc(implode("\n", $answer['variants'] ?? [])) ?></textarea>
          </div>
        <?php endforeach; ?>
        <div class="toolbar"><button type="submit" name="save_answers" value="1">Save Answers</button></div>
      </form>
    </div>

  </main>
</div>
</body>
</html>
