<?php
require_once '/home12/fiberule/public_html/kartikthanaweddingdinner/config/db.php';

// Set headers to force download as CSV
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=wedding_wishes_' . date('Y-m-d') . '.csv');

// Open the output stream
$output = fopen('php://output', 'w');

// Set the column headers for Excel
fputcsv($output, ['ID', 'Guest Name', 'Source', 'Message', 'Date Received']);

// Fetch all wishes from the database
$query = $pdo->query("SELECT id, name, source, message, created_at FROM wishes ORDER BY created_at DESC");

while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
    fputcsv($output, $row);
}

fclose($output);
exit;
?>