<?php
require_once __DIR__ . '/../config.php';
require_admin();
$rows = get_submissions();
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="submissions.csv"');
$out = fopen('php://output', 'w');
fputcsv($out, ['Name', 'Email', 'Platform', 'Answer', 'Details', 'Correct', 'Winner', 'Timestamp', 'IP']);
foreach ($rows as $row) {
  fputcsv($out, [
    $row['name'] ?? '',
    $row['email'] ?? '',
    $row['platform'] ?? '',
    $row['answer'] ?? '',
    $row['details'] ?? '',
    !empty($row['correct']) ? 'Yes' : 'No',
    !empty($row['is_winner']) ? 'Yes' : 'No',
    $row['timestamp'] ?? '',
    $row['ip'] ?? '',
  ]);
}
fclose($out);
exit;
