<?php

declare(strict_types=1);

namespace App\Framework\Console\Security;

use App\Framework\Console\Security\Middleware\SecurityMiddleware;
use App\Framework\DI\Attributes\Initializer;
use App\Framework\DI\Container;
use App\Framework\Environment\Environment;
use App\Framework\Environment\EnvKey;

final readonly class SecurityInitializer
{
    public function __construct(
        private Environment $environment
    ) {
    }

    #[Initializer]
    public function initialize(Container $container): void
    {
        // Register user provider
        $container->bind(UserProvider::class, EnvironmentUserProvider::class);

        // Register security service
        $container->singleton(SecurityService::class, function (Container $container) {
            return new SecurityService(
                $container->get(UserProvider::class)
            );
        });

        // Register security middleware
        $container->singleton(SecurityMiddleware::class, function (Container $container) {
            $enabled = $this->environment->getBool(EnvKey::CONSOLE_SECURITY_ENABLED, true);

            return new SecurityMiddleware(
                securityService: $container->get(SecurityService::class),
                attributeScanner: $container->get(\App\Framework\Discovery\AttributeScanner::class),
                enabled: $enabled
            );
        });
    }
}
