<?php

declare(strict_types=1);

use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\DefaultTwoFactorFormRenderer;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticator;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticatorInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticatorTwoFactorProvider;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleTotpFactory;
use Scheb\TwoFactorBundle\Security\TwoFactor\Validator\Constraints\UserGoogleTotpCodeValidator;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $container): void {
    $container->services()

        ->set('scheb_two_factor.security.google_totp_factory', GoogleTotpFactory::class)
            ->args([
                '%scheb_two_factor.google.server_name%',
                '%scheb_two_factor.google.issuer%',
                '%scheb_two_factor.google.digits%',
                (new ReferenceConfigurator('clock'))->nullOnInvalid(),
            ])

        ->set('scheb_two_factor.security.google_authenticator', GoogleAuthenticator::class)
            ->public()
            ->args([
                service('scheb_two_factor.security.google_totp_factory'),
                service('event_dispatcher'),
                '%scheb_two_factor.google.leeway%',
            ])

        ->set('scheb_two_factor.security.google.default_form_renderer', DefaultTwoFactorFormRenderer::class)
            ->lazy(true)
            ->args([
                service('twig'),
                '%scheb_two_factor.google.template%',
            ])

        ->set('scheb_two_factor.security.google.provider', GoogleAuthenticatorTwoFactorProvider::class)
            ->tag('scheb_two_factor.provider', ['alias' => 'google'])
            ->args([
                service('scheb_two_factor.security.google_authenticator'),
                service('scheb_two_factor.security.google.form_renderer'),
            ])

        ->set('scheb_two_factor.security.totp.validator.user_google_totp_code', UserGoogleTotpCodeValidator::class)
            ->tag('validator.constraint_validator', ['alias' => 'scheb_two_factor.security.totp.validator.user_google_totp_code'])
            ->args([
                service('security.token_storage'),
                service('scheb_two_factor.security.google_authenticator'),
            ])

        ->alias('scheb_two_factor.security.google.form_renderer', 'scheb_two_factor.security.google.default_form_renderer')

        ->alias(GoogleAuthenticatorInterface::class, 'scheb_two_factor.security.google_authenticator')

        ->alias(GoogleAuthenticator::class, 'scheb_two_factor.security.google_authenticator');
};
