<?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/ApiResponse.php';
require_once __DIR__ . '/../../app/core/ApiAuth.php';
require_once __DIR__ . '/../../app/core/Request.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/Categoria.php';
require_once __DIR__ . '/../../app/models/Ocorrencia.php';
require_once __DIR__ . '/../../app/models/ApiToken.php';
require_once __DIR__ . '/../../app/models/WhatsAppConfig.php';
require_once __DIR__ . '/../../app/models/WhatsAppLog.php';
require_once __DIR__ . '/../../app/models/WhatsAppDispatch.php';

require_once __DIR__ . '/../../app/controllers/api/ApiAuthController.php';
require_once __DIR__ . '/../../app/controllers/api/ApiCategoriaController.php';
require_once __DIR__ . '/../../app/controllers/api/ApiOcorrenciaController.php';
require_once __DIR__ . '/../../app/controllers/api/ApiConsultaController.php';
require_once __DIR__ . '/../../app/services/WhatsAppService.php';
require_once __DIR__ . '/../../app/controllers/api/ApiWhatsAppController.php';

$router = new Router();
require_once __DIR__ . '/../../config/api_routes.php';

if (empty($_GET['route']) && isset($_SERVER['PATH_INFO']) && is_string($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] !== '') {
    $_GET['route'] = $_SERVER['PATH_INFO'];
}

try {
    $router->dispatch($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']);
} catch (Throwable $e) {
    ApiResponse::error(APP_DEBUG ? $e->getMessage() : 'Erro interno da API.', [], 500);
}
