<?php
require_once '../../config.php';
require_once __DIR__ . '/lib/report_data.php';
require_once __DIR__ . '/templates/registry.php';

$conn = getDBConnection();

$student_id    = (int) ($_GET['student_id'] ?? 0);
$term          = $_GET['term'] ?? getCurrentTerm();
$academic_year = $_GET['academic_year'] ?? getAcademicYear();

$data = getReportData($conn, $student_id, (string) $term, (string) $academic_year);
$conn->close();

if (empty($data['student'])) {
    die("Student not found");
}

// A per-view ?template= override wins; otherwise use the school default.
$templateKey = $_GET['template'] ?? ($data['settings']['report_template'] ?? 'ges_standard');
$render = resolveReportRenderer($templateKey);
$html = $render($data, 'print');

// Auto-trigger the browser print dialog once the page loads.
$html = str_replace('</body>', '<script>window.onload=function(){window.print();};</script></body>', $html);

echo $html;
