<?php
namespace Misuzu;

use Index\Http\{HttpResponseBuilder,HttpRequest};
use Index\Http\ErrorHandling\HtmlErrorHandler;

class RoutingErrorHandler extends HtmlErrorHandler {
    public function handle(HttpResponseBuilder $response, HttpRequest $request, int $code, string $message): void {
        if(str_starts_with($request->getPath(), '/_')) {
            $response->setTypePlain();
            $response->setContent(sprintf('HTTP %03d', $code));
            return;
        }

        if($code === 500 || $code === 503) {
            $response->setTypeHTML();
            $response->setContent(file_get_contents(sprintf('%s/%03d.html', MSZ_TEMPLATES, $code)));
            return;
        }

        if($code === 401 || $code === 403 || $code === 404) {
            $response->setTypeHTML();
            $response->setContent(Template::renderRaw(sprintf('errors.%03d', $code)));
            return;
        }

        parent::handle($response, $request, $code, $message);
    }
}
