<?php

namespace App\Framework\Config;

use App\Framework\Context\ContextType;
use App\Framework\DateTime\Timezone;
use App\Framework\DI\Container;

final readonly class TypedConfigInitializer
{
    public function __construct(
        private Environment $env,
    ){
        #debug($env->getEnum('APP_ENV', EnvironmentType::class, EnvironmentType::PROD));
    }

    public function __invoke(Container $container): TypedConfiguration
    {
        $appConfig = $this->createAppConfig();

        $container->instance(AppConfig::class, $appConfig);

        return new TypedConfiguration(
            app: $appConfig,
        );
    }

    private function createAppConfig(): AppConfig
    {
        return new AppConfig(
            environment: $this->env->getString(
                key: EnvKey::APP_ENV,
                default: 'production'
            ),

            debug: $this->env->getBool(EnvKey::APP_DEBUG),

            timezone: $this->env->getEnum(
                key: EnvKey::APP_TIMEZONE,
                enumClass: Timezone::class,
                default: Timezone::EuropeBerlin
            ),

            locale: $this->env->getString(EnvKey::APP_LOCALE, 'de'),
        );
    }
}
