<?php
declare(strict_types=1);

define('BASE_PATH', str_replace('\\', '/', dirname(__DIR__)));
define('APP_PATH', BASE_PATH . '/app');
define('CONFIG_PATH', BASE_PATH . '/config');
define('VIEWS_PATH', APP_PATH . '/Views');
define('STORAGE_PATH', BASE_PATH . '/storage');

require APP_PATH . '/Autoload.php';
require APP_PATH . '/helpers.php';

if (is_ajax()) {
    ini_set('display_errors', '0');
    ob_start();
}

set_exception_handler(static function (Throwable $e): void {
    $debug = (bool) config('app.debug', false);
    http_response_code(500);
    if (PHP_SAPI !== 'cli') {
        header('Content-Type: text/html; charset=utf-8');
    }
    $msg = $debug ? $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine() : 'Something went wrong.';
    error_log($e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine());
    echo '<pre style="font-family:Segoe UI,sans-serif;padding:24px">' . htmlspecialchars($msg, ENT_QUOTES, 'UTF-8') . '</pre>';
});
