<?php

declare(strict_types=1);

namespace MauticPlugin\DealFlowBundle;

use Mautic\IntegrationsBundle\Bundle\AbstractPluginBundle;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class DealFlowBundle extends AbstractPluginBundle
{
    public function build(ContainerBuilder $container): void
    {
        parent::build($container);

        // Register short aliases for FQCN-only services so Mautic's old-style
        // config (which treats backslash-containing strings as literals) can
        // reference them in service arguments.
        // Also remove value objects that Symfony tries to auto-wire as services.
        $container->addCompilerPass(new class implements CompilerPassInterface {
            public function process(ContainerBuilder $container): void
            {
                if ($container->has(\Mautic\CoreBundle\Service\GlobalSearch::class)) {
                    $container->setAlias('dealflow.service.global_search', \Mautic\CoreBundle\Service\GlobalSearch::class);
                }

                // Value objects are not services — remove them from the container
                foreach ([
                    Service\LicenseResult::class,
                    Service\UpdateCheckResult::class,
                    Service\UpdateResult::class,
                ] as $valueObject) {
                    if ($container->hasDefinition($valueObject)) {
                        $container->removeDefinition($valueObject);
                    }
                }
            }
        });
    }
}
