<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\AdminBundle\Form\Offer;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Ferienpass\AdminBundle\Form\AsOfferFormType;
use Ferienpass\CoreBundle\Entity\Edition as EditionEntity;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;

#[AsOfferFormType]
class Edition extends AbstractType
{
    public function __construct(private readonly TranslatorInterface $translator)
    {
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'section' => 'default',
            'required' => true,
            'class' => EditionEntity::class,
            'query_builder' => static function (EntityRepository $er): QueryBuilder {
                return $er->createQueryBuilder('e')
                    ->orderBy('e.name', 'ASC');
            },
            'group_by' => function (EditionEntity $edition): string {
                return $edition->isArchived() ? $this->translator->trans('editions.group.archived', domain: 'admin') : $this->translator->trans('editions.group.active', domain: 'admin');
            },
            'choice_label' => 'name',
            'multiple' => false,
            'expanded' => false,
            'placeholder' => '-',
            'help' => 'offers.help.edition',
        ]);
    }

    public function getParent(): string
    {
        return EntityType::class;
    }
}
