<?php

declare(strict_types=1);

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin;

use CpCreation\VitiCore\TransactionalEmail\Model\TransactionalEmail;
use CpCreation\VitiCore\TransactionalEmail\TransactionalEmailRecipient;
use CpCreation\VitiCore\TransactionalEmail\TransactionalEmailTrigger;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class TransactionalEmailType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', TextType::class, [
                'label' => 'Nom interne',
                'required' => true,
            ])
            ->add('triggerEvent', ChoiceType::class, [
                'label' => 'Action déclencheuse',
                'choices' => TransactionalEmailTrigger::choices(),
                'required' => true,
            ])
            ->add('delayDays', IntegerType::class, [
                'label' => 'Délai après action (jours)',
                'required' => true,
                'attr' => [
                    'min' => 0,
                ],
            ])
            ->add('recipientType', ChoiceType::class, [
                'label' => 'Destinataire',
                'choices' => TransactionalEmailRecipient::choices(),
                'required' => true,
            ])
            ->add('templateCode', TextType::class, [
                'label' => 'Code template',
                'required' => false,
                'help' => 'Optionnel pour le moment, sera utilisé quand les templates seront branchés.',
            ])
            ->add('description', TextareaType::class, [
                'label' => 'Notes de logique',
                'required' => false,
                'attr' => [
                    'rows' => 5,
                ],
            ])
            ->add('disabled', CheckboxType::class, [
                'label' => 'Désactivé',
                'required' => false,
            ]);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => TransactionalEmail::class,
            'translation_domain' => 'form',
        ]);
    }
}
