<?php
declare(strict_types=1);

namespace App;

final class App
{
    private static ?self $instance = null;

    private Request $request;
    private SessionManager $session;
    private Assets $assets;
    private I18n $i18n;
    private Flash $flash;
    private ?\PDO $pdo;

    private function __construct(private Config $config)
    {
        $this->request = new Request();
        $this->session = new SessionManager($config);
        $this->assets  = new Assets($config);
        $this->i18n    = new I18n($config, 'en');
        $this->flash   = new Flash($this->session);
        $this->pdo     = Database::createPdo($config);
    }

    public static function init(Config $config): self
    {
        if (self::$instance === null) {
            self::$instance = new self($config);
        }
        return self::$instance;
    }

    public static function get(): self
    {
        if (self::$instance === null) {
            throw new \RuntimeException('App not initialized. Call App::init() in bootstrap.');
        }
        return self::$instance;
    }

    public function config(): Config { return $this->config; }
    public function request(): Request { return $this->request; }
    public function session(): SessionManager { return $this->session; }
    public function assets(): Assets { return $this->assets; }
    public function i18n(): I18n { return $this->i18n; }
    public function flash(): Flash { return $this->flash; }
    public function pdo(): ?\PDO { return $this->pdo; }
}
