<?php

namespace NoahVet\Reef\Symfony;

use NoahVet\Reef\DependencyInjection\Compiler\CachePoolTaggingCompilerPass;
use NoahVet\Reef\DependencyInjection\ReefApiClientExtension;
use NoahVet\Reef\Domain\Security\Policy\PolicyInterface;
use NoahVet\Reef\Domain\Security\Port\ResourceFinderInterface;
use NoahVet\Reef\Infrastructure\Security\IAM\Generator\InternalGeneratorInterface;
use NoahVet\Reef\Infrastructure\Security\IAM\Mapper\InternalResourceMapperInterface;
use NoahVet\Reef\Security\User\Provider\ReefOAuthUserProviderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class ReefApiClientBundle extends Bundle
{
    #[\Override]
    public function getContainerExtension(): ?ExtensionInterface
    {
        return new ReefApiClientExtension();
    }

    #[\Override]
    public function build(ContainerBuilder $container): void
    {
        $container->registerForAutoconfiguration(InternalResourceMapperInterface::class)
            ->addTag('reef.iam.mapper')
        ;
        $container->registerForAutoconfiguration(InternalGeneratorInterface::class)
            ->addTag('reef.iam.generator')
        ;
        $container->registerForAutoconfiguration(PolicyInterface::class)
            ->addTag('reef.iam.policy')
        ;
        $container->registerForAutoconfiguration(ResourceFinderInterface::class)
            ->addTag('reef.iam.finder')
        ;
        $container->registerForAutoconfiguration(ReefOAuthUserProviderInterface::class)
            ->addTag('reef.security.user_provider')
        ;
        $container->addCompilerPass(new CachePoolTaggingCompilerPass());
        if (\interface_exists('HWI\Bundle\OAuthBundle\Security\Core\User\OAuthAwareUserProviderInterface')) {
            $reefUserProviderDefinition = new Definition('NoahVet\Reef\HWI\OAuth\ReefUserProvider');
            $container->addDefinitions([
                'NoahVet\Reef\HWI\OAuth\ReefUserProvider' => $reefUserProviderDefinition,
            ]);
        }
    }
}
