<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

$fixtureExportRows = 'fixture_export_rows';
$fixtureRenderPdfDocument = 'fixture_render_pdf_document';

fixture_module_boot();

$competitionId = (int) ($_GET['competition_id'] ?? 0);
$fixtures = $fixtureExportRows($competitionId > 0 ? $competitionId : null);
$title = 'JLFC Fixtures';

if ($competitionId > 0) {
    $competition = fixture_competition_by_id($competitionId);
    if ($competition !== null) {
        $title = 'JLFC Fixtures - ' . $competition['name'];
    }
}

$filename = 'fixtures-' . date('Y-m-d') . '.pdf';

header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . $filename . '"');

echo $fixtureRenderPdfDocument($title, $fixtures);
exit;
