<?php

declare(strict_types=1);

namespace App\App;

use Psr\Container\ContainerInterface;

/**
 * Boots the UI Slim app. Owns nothing more than the wiring sequence:
 * load settings → validate → build container → return Slim App.
 *
 * `public/index.php` calls `Bootstrap::run()` at request time. Tests can
 * call `Bootstrap::container($overrides)` to obtain a wired container
 * without touching `$_SERVER`.
 */
final class Bootstrap
{
    /**
     * @return \Slim\App<ContainerInterface|null>
     */
    public static function run(): \Slim\App
    {
        /** @var array<string, mixed> $settings */
        $settings = require __DIR__ . '/../../config/settings.php';
        Config::validateOrExit($settings);

        $container = Container::build($settings);

        return AppFactory::build($container);
    }

    /**
     * Test helper: build a container with optional setting overrides.
     * Skips the fail-fast validation so tests can run with empty values.
     *
     * @param array<string, mixed> $overrides
     */
    public static function container(array $overrides = []): ContainerInterface
    {
        /** @var array<string, mixed> $settings */
        $settings = require __DIR__ . '/../../config/settings.php';
        $settings = array_replace($settings, $overrides);

        return Container::build($settings);
    }
}
