<?php

namespace KupShop\KupShopBundle;

use KupShop\KupShopBundle\Attribute\OverrideService;
use KupShop\KupShopBundle\DependencyInjection\Compiler\EmailServicesPass;
use KupShop\KupShopBundle\DependencyInjection\Compiler\KupShopContainerPass;
use KupShop\KupShopBundle\DependencyInjection\Compiler\OverrideServicePass;
use KupShop\KupShopBundle\Email\EmailInterface;
use KupShop\KupShopBundle\EmailRegister\EmailRegisterInterface;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class KupShopBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        // Emails
        $container->registerForAutoconfiguration(EmailInterface::class)
            ->addTag('kupshop.email')
            ->setPublic(false)
            ->setAutowired(true);

        $container->registerForAutoconfiguration(EmailRegisterInterface::class)
            ->addTag('kupshop.email.register')
            ->setPublic(false)
            ->setAutowired(true);

        $container->addCompilerPass(new EmailServicesPass());

        // Service Override
        $container->registerAttributeForAutoconfiguration(OverrideService::class, function (ChildDefinition $definition, OverrideService $attribute, \Reflector $reflection) {
            $definition->setAbstract(true)
                ->addTag('kupshop.override_service', ['service' => $attribute->getService()]);
        });

        $container->addCompilerPass(new OverrideServicePass());

        // KupShop container
        $container->addCompilerPass(new KupShopContainerPass());
    }
}
