<?php
declare(strict_types=1);

namespace PSLauncher\Routes;

use PSLauncher\Response;

final class Releasenotes
{
    public static function handle(string $version): void
    {
        if (!preg_match('/^[0-9]+\.[0-9]+\.[0-9]+$/', $version)) {
            Response::error('invalid_version', 'Bad version format', 400);
        }
        $file = dirname(__DIR__, 2) . "/releasenotes/{$version}.md";
        if (!is_file($file)) {
            Response::error('not_found', "Release notes for {$version} not found", 404);
        }
        header('Cache-Control: public, max-age=3600, must-revalidate');
        header('Content-Type: text/markdown; charset=utf-8');
        header('Content-Length: ' . filesize($file));
        readfile($file);
    }
}
