<?php

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Contao\CoreBundle\Asset\ContaoContext;
use Contao\CoreBundle\Migration\MigrationInterface;
use Contao\CoreBundle\OptIn\OptInInterface;
use Ferienpass\CmsBundle\Controller\Fragment\ChangePasswordController;
use Ferienpass\CmsBundle\Controller\Fragment\CloseAccount;
use Ferienpass\CmsBundle\Controller\Fragment\ConsentsController;
use Ferienpass\CmsBundle\Controller\Fragment\DataUsageController;
use Ferienpass\CmsBundle\Controller\Fragment\OfferDetailsController;
use Ferienpass\CmsBundle\Controller\Fragment\ParticipantsController;
use Ferienpass\CmsBundle\Controller\Fragment\PasskeysController;
use Ferienpass\CmsBundle\Controller\Fragment\PaymentsController;
use Ferienpass\CmsBundle\Controller\Fragment\PersonalDataController;
use Ferienpass\CmsBundle\Filter\Type\FilterType;
use Ferienpass\CmsBundle\Filter\Type\Offer\AgeType;
use Ferienpass\CmsBundle\Filter\Type\Offer\BaseType;
use Ferienpass\CmsBundle\Filter\Type\Offer\CategoryType;
use Ferienpass\CmsBundle\Filter\Type\Offer\EarliestDateType;
use Ferienpass\CmsBundle\Filter\Type\Offer\FeeType;
use Ferienpass\CmsBundle\Filter\Type\Offer\FreeSeatsType;
use Ferienpass\CmsBundle\Filter\Type\Offer\FreeType;
use Ferienpass\CmsBundle\Filter\Type\Offer\LatestDateType;
use Ferienpass\CmsBundle\Filter\Type\Offer\NameType;
use Ferienpass\CmsBundle\Filter\Type\Offer\NoApplicationType;
use Ferienpass\CmsBundle\Filter\Type\Offer\WheelchairAccessibleType;
use Ferienpass\CmsBundle\Menu\MenuBuilder;
use Ferienpass\CmsBundle\Page\PageBuilderFactory;
use Ferienpass\CmsBundle\Routing\LocaleContentUrlGeneratorDecorator;
use Ferienpass\CmsBundle\Routing\LocaleRouteProviderDecorator;
use Ferienpass\CmsBundle\Voter\ContentElementsVoter;
use Ferienpass\CmsBundle\Voter\FormFieldsVoter;
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;

return function(ContainerConfigurator $container): void {
    $services = $container->services()
        ->defaults()
        ->autoconfigure()
        ->autowire()
    ;

    $services
        ->load('Ferienpass\\CmsBundle\\', '../src/')
        ->exclude('../src/{DependencyInjection,Entity}')
    ;

    // Tags by interface
    $services
        ->instanceof(MigrationInterface::class)
        ->tag('contao.migration')
    ;
    $services
        ->instanceof(FilterType::class)
        ->tag(FilterType::class)
    ;

    #AutoconfigureTag not working (imho)
    $services->get(AgeType::class)->tag(FilterType::class);
    $services->get(BaseType::class)->tag(FilterType::class);
    $services->get(CategoryType::class)->tag(FilterType::class);
    $services->get(EarliestDateType::class)->tag(FilterType::class);
    $services->get(FeeType::class)->tag(FilterType::class);
    $services->get(LatestDateType::class)->tag(FilterType::class);
    $services->get(NameType::class)->tag(FilterType::class);
    $services->get(FreeSeatsType::class)->tag(FilterType::class);
    $services->get(NoApplicationType::class)->tag(FilterType::class);
    $services->get(FreeType::class)->tag(FilterType::class);
    $services->get(WheelchairAccessibleType::class)->tag(FilterType::class);

    // Tags by directory.
    $services
        ->load('Ferienpass\\CmsBundle\\Controller\\Fragment\\', '../src/Controller/Fragment/')
        ->tag('ferienpass.fragment')
    ;

    $services->set(OfferDetailsController::class)
        ->autowire()
        ->tag('ferienpass.fragment', ['renderer' => 'esi'])
    ;

    // Aliases for autowiring
    $services->alias(ContaoContext::class, 'contao.assets.files_context');
    $services->alias(OptInInterface::class, 'contao.opt_in');
    $services->alias(LogoutUrlGenerator::class, 'security.logout_url_generator');
    $services->alias(FragmentHandler::class, 'contao.fragment.handler');

    // Tagged user account fragments
    $services->get(PersonalDataController::class)
        ->tag('ferienpass.user_account', ['key' => 'personal_data', 'alias' => 'account', 'icon' => 'user-circle', 'priority' => 100, 'label' => 'personalData'])
    ;

    $services->get(ParticipantsController::class)
        ->tag('ferienpass.user_account', ['key' => 'participants', 'alias' => 'account', 'icon' => 'user-group', 'priority' => 90])
    ;

    $services->get(ChangePasswordController::class)
        ->tag('ferienpass.user_account', ['key' => 'change_password', 'alias' => 'account', 'icon' => 'lock-closed', 'priority' => 30, 'label' => 'changePassword'])
    ;

    $services->get(PasskeysController::class)
        ->tag('ferienpass.user_account', ['key' => 'passkeys', 'alias' => 'account', 'icon' => 'finger-print', 'priority' => 25, 'label' => 'passkeys'])
    ;

    $services->get(CloseAccount::class)
        ->tag('ferienpass.user_account', ['key' => 'close_account', 'alias' => 'account', 'icon' => 'trash', 'priority' => 0, 'label' => 'closeAccount'])
    ;

    $services->get(ConsentsController::class)
        ->tag('ferienpass.user_account', ['key' => 'consents', 'alias' => 'einwilligungen', 'icon' => 'currency-euro', 'priority' => 10, 'label' => 'consents'])
    ;

    $services->get(DataUsageController::class)
        ->tag('ferienpass.user_account', ['key' => 'data_usage', 'alias' => 'datennutzung', 'icon' => 'share', 'priority' => 8, 'label' => 'dataUsage'])
    ;

    $services->get(id: PaymentsController::class)
        ->tag('ferienpass.user_account', ['key' => 'payments', 'alias' => 'zahlungen', 'icon' => 'credit-card', 'priority' => 5, 'label' => 'payments'])
    ;

    // Tagged menu builders
    $services->set(MenuBuilder::class)
        ->tag('knp_menu.menu_builder', ['method' => 'userNavigation', 'alias' => 'user_navigation'])
        ->tag('knp_menu.menu_builder', ['method' => 'userAccountNavigation', 'alias' => 'user_account_navigation'])
    ;

    // Voters need higher priority
    $services->get(ContentElementsVoter::class)->tag('security.voter', ['priority' => 100]);
    $services->get(FormFieldsVoter::class)->tag('security.voter', ['priority' => 100]);

    // Public services
    $services->set(PageBuilderFactory::class)
        ->public()
    ;

    // Locale-prefixed CMF routing: decorate Contao's RouteProvider to apply
    // locale URL prefixes (de='', nl='/nl', en='/en', ...) at runtime.
    $services->set(LocaleRouteProviderDecorator::class)
        ->decorate('contao.routing.route_provider')
        ->args([service('.inner')])
    ;

    $services->set(LocaleContentUrlGeneratorDecorator::class)
        ->decorate('contao.routing.content_url_generator')
        ->autowire()
        ->args([service('.inner')])
    ;
};
