<?php

declare(strict_types=1);

use App\Core\App;
use App\Core\ExceptionHandler;
use App\Core\Request;
use App\Core\Session;
use App\Core\Router;
use App\Core\View;
use Dotenv\Dotenv;

require dirname(__DIR__) . '/vendor/autoload.php';

$root = dirname(__DIR__);

if (is_file($root . '/.env')) {
    Dotenv::createImmutable($root)->safeLoad();
}

date_default_timezone_set($_ENV['APP_TIMEZONE'] ?? 'Africa/Nairobi');
Session::start();

ExceptionHandler::register(
    debug: filter_var($_ENV['APP_DEBUG'] ?? false, FILTER_VALIDATE_BOOL),
    logFile: $root . '/storage/logs/app.log'
);

$router = new Router();

foreach ([$root . '/routes/web.php', $root . '/routes/api.php'] as $routeFile) {
    if (is_file($routeFile)) {
        $register = require $routeFile;
        if (is_callable($register)) {
            $register($router);
        }
    }
}

$app = new App($router, new View($root . '/resources/views'));
$app->run(Request::capture());
