<?php
declare(strict_types=1);

// 1) Load config (constants, env, domains)
require_once __DIR__ . '/config.php';

// 2) Composer Autoloader (falls vorhanden)
$composerAutoload = __DIR__ . '/../vendor/autoload.php';
if (file_exists($composerAutoload)) {
    require_once $composerAutoload;
} else {
    // 2b) Fallback: minimaler Autoloader
    spl_autoload_register(function (string $class): void {
        $prefix = 'App\\';
        if (!str_starts_with($class, $prefix)) {
            return;
        }

        $rel  = substr($class, strlen($prefix));
        $path = __DIR__ . '/../src/App/' . str_replace('\\', '/', $rel) . '.php';

        if (file_exists($path)) {
            require_once $path;
        }
    });
}

// 3) Global helper functions (tpl(), t(), asset_*())
require_once __DIR__ . '/../src/helpers.php';

// 4) Initialize App (services)
$config = \App\Config::fromPhpConstants(__DIR__ . '/../config');
\App\App::init($config);

// 5) Start session + create client-id cookie
$app = \App\App::get();
$app->session()->start();
$clientId = $app->session()->ensureClientId();

// Optionally expose a single global for templates if desired
$GLOBALS['client_id'] = $clientId;
