<?php

declare(strict_types=1);

require_once __DIR__ . '/../config/config.php';

if (defined('APP_DEBUG') && APP_DEBUG) {
    ini_set('display_errors', '1');
    ini_set('display_startup_errors', '1');
    error_reporting(E_ALL);
}

require_once __DIR__ . '/../app/helpers/php74.php';
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../app/core/Router.php';
require_once __DIR__ . '/../app/core/Controller.php';
require_once __DIR__ . '/../app/core/Model.php';
require_once __DIR__ . '/../app/core/Session.php';
require_once __DIR__ . '/../app/core/Auth.php';

require_once __DIR__ . '/../app/helpers/url.php';
require_once __DIR__ . '/../app/helpers/protocol.php';
require_once __DIR__ . '/../app/helpers/upload.php';

require_once __DIR__ . '/../app/models/Setor.php';
require_once __DIR__ . '/../app/models/OcorrenciaHistorico.php';
require_once __DIR__ . '/../app/controllers/AuthController.php';
require_once __DIR__ . '/../app/controllers/DashboardController.php';
require_once __DIR__ . '/../app/controllers/ServicoController.php';
require_once __DIR__ . '/../app/controllers/UsuarioController.php';
require_once __DIR__ . '/../app/controllers/OcorrenciaController.php';
require_once __DIR__ . '/../app/controllers/ConsultaController.php';

$router = new Router();

require_once __DIR__ . '/../config/routes.php';

try {
    $routeCandidate = isset($_GET['route']) ? (string) $_GET['route'] : (isset($_SERVER['PATH_INFO']) ? (string) $_SERVER['PATH_INFO'] : (string) $_SERVER['REQUEST_URI']);
    $router->dispatch($routeCandidate, $_SERVER['REQUEST_METHOD']);
} catch (Throwable $e) {
    http_response_code(500);

    if (defined('APP_DEBUG') && APP_DEBUG) {
        echo '<h1>Erro interno do sistema</h1>';
        echo '<pre style="white-space:pre-wrap;font-family:monospace">' . htmlspecialchars($e->getMessage() . "\n\n" . $e->getTraceAsString(), ENT_QUOTES, 'UTF-8') . '</pre>';
        exit;
    }

    echo 'Erro interno do sistema.';
}
