<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Core\Request;
use App\Core\Response;
use App\Core\View;
use App\Models\RedirectRepository;

final class ErrorController
{
    /** @param array<string,string> $params */
    public function notFound(Request $request, array $params): Response
    {
        $redirect = RedirectRepository::find($request->path);
        if ($redirect !== null) {
            $code = (int) $redirect['code'];
            if ($code === 410) {
                return Response::html('', 410);
            }
            return Response::redirect((string) $redirect['new_path'], $code === 302 ? 302 : 301);
        }

        return Response::html(
            View::render('errors/404', ['pageTitle' => 'Page introuvable']),
            404
        );
    }
}
