<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/bootstrap.php';
require_admin();
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="enquiries-' . date('Y-m-d') . '.csv"');
$out = fopen('php://output', 'w');
fputcsv($out, ['id', 'created_at', 'name', 'email', 'phone', 'subject', 'message', 'status']);
$st = db()->query('SELECT * FROM enquiries ORDER BY created_at DESC');
while ($row = $st->fetch()) {
    fputcsv($out, [
        $row['id'],
        $row['created_at'],
        $row['name'],
        $row['email'],
        $row['phone'] ?? '',
        $row['subject'] ?? '',
        $row['message'] ?? '',
        $row['status'],
    ]);
}
fclose($out);
exit;
