<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\EventBundle\Form;

use Chill\EventBundle\Entity\EventTheme;
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\AbstractType;

class EventThemeType extends AbstractType
{
    public function __construct(protected TranslatableStringHelperInterface $translatableStringHelper) {}

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', TranslatableStringFormType::class, [
                'label' => 'Nom',
            ]);

        if ('create' === $options['step']) {
            $builder
                ->add('parent', EntityType::class, [
                    'class' => EventTheme::class,
                    'required' => false,
                    'choice_label' => fn (EventTheme $theme): ?string => $this->translatableStringHelper->localize($theme->getName()),
                    'mapped' => 'create' == $options['step'],
                ]);
        }

        $builder
            ->add('ordering', NumberType::class, [
                'required' => true,
                'scale' => 6,
            ])
            ->add('isActive', ChoiceType::class, [
                'choices' => [
                    'Yes' => true,
                    'No' => false,
                ],
                'expanded' => true,
            ]);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => EventTheme::class,
        ]);
        $resolver->setRequired('step')
            ->setAllowedValues('step', ['create', 'edit']);
    }
}
