<?php
/**
 * TiDo - public entrypoint for each <client>.tido.sk/public_html.
 * Shared Laravel is on zalozny.tido.sk, NOT zakazny.tido.sk.
 */
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;

define('LARAVEL_START', microtime(true));

if (PHP_VERSION_ID < 80300) {
    http_response_code(503);
    header('Content-Type: text/plain; charset=utf-8');
    exit('TiDo: domena vyzaduje PHP 8.3 alebo novsie (odporucane 8.4).');
}

$sharedApp = dirname(__DIR__, 2) . '/zalozny.tido.sk/tido_app';
$_SERVER['TIDO_TENANT_CONFIG_PATH'] = __DIR__ . '/a_config.php';

if (!is_file(__DIR__ . '/a_config.php')) {
    http_response_code(503);
    header('Content-Type: text/plain; charset=utf-8');
    exit('TiDo: chyba klientsky a_config.php.');
}

$autoload = $sharedApp . '/vendor/autoload.php';
if (!is_file($autoload) || !is_readable($autoload)) {
    http_response_code(503);
    header('Content-Type: text/plain; charset=utf-8');
    exit('TiDo: chyba vendor/autoload.php v zalozny.tido.sk/tido_app.');
}
if (!is_file($sharedApp . '/bootstrap/app.php')) {
    http_response_code(503);
    header('Content-Type: text/plain; charset=utf-8');
    exit('TiDo: chyba bootstrap/app.php v spolocnom Laraveli.');
}

try {
    if (is_file($maintenance = $sharedApp . '/storage/framework/maintenance.php')) {
        require $maintenance;
    }
    require $autoload;
    /** @var Application $app */
    $app = require $sharedApp . '/bootstrap/app.php';
    $app->handleRequest(Request::capture());
} catch (Throwable $exception) {
    error_log('[TiDo shared bootstrap ' . ($_SERVER['HTTP_HOST'] ?? 'unknown') . '] ' . $exception);
    if (!headers_sent()) {
        http_response_code(500);
        header('Content-Type: text/plain; charset=utf-8');
        header('Cache-Control: no-store');
    }
    echo 'TiDo: chyba pri starte aplikacie. Podrobnosti su vo Websupport error_log.';
}
