<?php
declare(strict_types=1);
namespace App\Core;

final class Router {
    private array $get = [];
    private array $post = [];

    public function get(string $page, array $handler): void { $this->get[$page] = $handler; }
    public function post(string $page, array $handler): void { $this->post[$page] = $handler; }

    public function dispatch(string $page, string $method): void {
        $map = strtoupper($method) === 'POST' ? $this->post : $this->get;
        $handler = $map[$page] ?? null;
        if (!$handler) {
            http_response_code(404);
            include __DIR__ . '/../Views/errors/404.php';
            return;
        }
        [$class, $action] = $handler;
        $controller = app()->get($class);
        $controller->$action();
    }
}
