<?php
/**
 * Export TFE listings as CSV.
 *
 * Thin dispatcher — delegates all data assembly to ExportController,
 * then streams the response.
 */
require_once __DIR__ . "/../../../bootstrap.php";
require_once __DIR__ . '/../../../src/AdminAuth.php';
AdminAuth::requireLogin();

require_once APP_ROOT . '/src/Controllers/ExportController.php';
$controller = ExportController::create();

$filename = 'posterg-export-' . date('Y-m-d') . '.csv';

header('Content-Type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Cache-Control: no-cache, must-revalidate');

// UTF-8 BOM for Excel compatibility
echo "\xEF\xBB\xBF";

$out = fopen('php://output', 'w');

// Column headers
fputcsv($out, ExportController::CSV_HEADERS, ',', '"', '');

// Data rows
$rows = $controller->exportAllTheses();
foreach ($rows as $csvLine) {
    fputcsv($out, $csvLine, ',', '"', '');
}

fclose($out);
exit;
