<?php

declare(strict_types=1);

final class StatisticsController
{
    public static function index(): void
    {
        $requested = (string) ($_GET['year'] ?? date('Y'));
        $all = $requested === 'all';
        $year = $all ? null : max(2000, min(2200, (int) $requested));
        $years = StatisticsService::years();
        $municipalities = StatisticsService::municipalities();
        $municipality = trim((string) ($_GET['municipality'] ?? ''));
        if ($municipality !== '' && !isset($municipalities[$municipality])) {
            $municipality = '';
        }
        $ids = $municipality !== '' ? StatisticsService::animalIdsForMunicipality($municipality) : null;
        render('statistics.php', [
            'title' => t('statistics.title'),
            'pageDescription' => t('statistics.description'),
            'year' => $year,
            'allYears' => $all,
            'years' => $years,
            'municipality' => $municipality,
            'municipalities' => $municipalities,
            'municipalityCoverage' => StatisticsService::municipalityCoverage(),
            'stats' => $all ? StatisticsService::all($ids) : StatisticsService::annual($year, $ids),
            'comparison' => $all ? [] : StatisticsService::comparison($year, $ids),
            'timeline' => StatisticsService::timeline($years, $ids),
            'undatedHandled' => StatisticsService::undatedHandled($ids),
            'undatedDeaths' => StatisticsService::undatedDeaths($ids),
            'undatedBirths' => StatisticsService::undatedBirths($ids),
            'undatedExits' => StatisticsService::undatedExits($ids),
            'scopeCounts' => StatisticsService::scopeCounts($ids),
            'distributions' => StatisticsService::distributions($year, $ids, $municipality ?: null),
            'health' => StatisticsService::currentHealth($ids),
            'costs' => StatisticsService::costs($year, $ids),
            'municipalityCosts' => StatisticsService::costsByMunicipality($year),
        ]);
    }
}
