<?php

declare(strict_types=1);

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

use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use CpCreation\VitiCore\Marketing\Model\AbandonedCartConfiguration;
use CpCreation\VitiCore\Marketing\Model\Translation\AbandonedCartConfigurationTranslation;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class AbandonedCartConfigurationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('enabled', CheckboxType::class, [
                'label' => 'marketing.abandoned_cart.configuration.enabled',
                'help' => 'marketing.abandoned_cart.configuration.enabled_help',
                'required' => false,
            ])
            ->add('translations', TranslationsType::class, [
                'label' => false,
                'fields' => [
                    'emailTitle' => [
                        'field_type' => TextType::class,
                        'label' => 'marketing.abandoned_cart.configuration.email_title',
                        'help' => 'marketing.abandoned_cart.configuration.email_title_help',
                        'help_translation_parameters' => [
                            '%max%' => AbandonedCartConfigurationTranslation::TITLE_MAX_LENGTH,
                        ],
                        'required' => false,
                        'attr' => [
                            'maxlength' => AbandonedCartConfigurationTranslation::TITLE_MAX_LENGTH,
                            'data-character-counter' => true,
                            'autocomplete' => 'off',
                        ],
                    ],
                    'emailSubject' => [
                        'field_type' => TextType::class,
                        'label' => 'marketing.abandoned_cart.configuration.email_subject',
                        'help' => 'marketing.abandoned_cart.configuration.email_subject_help',
                        'help_translation_parameters' => [
                            '%max%' => AbandonedCartConfigurationTranslation::SUBJECT_MAX_LENGTH,
                        ],
                        'required' => false,
                        'attr' => [
                            'maxlength' => AbandonedCartConfigurationTranslation::SUBJECT_MAX_LENGTH,
                            'data-character-counter' => true,
                            'autocomplete' => 'off',
                        ],
                    ],
                    'emailPreheader' => [
                        'field_type' => TextType::class,
                        'label' => 'marketing.abandoned_cart.configuration.email_preheader',
                        'help' => 'marketing.abandoned_cart.configuration.email_preheader_help',
                        'help_translation_parameters' => [
                            '%max%' => AbandonedCartConfigurationTranslation::PREHEADER_MAX_LENGTH,
                        ],
                        'required' => false,
                        'attr' => [
                            'maxlength' => AbandonedCartConfigurationTranslation::PREHEADER_MAX_LENGTH,
                            'data-character-counter' => true,
                            'autocomplete' => 'off',
                        ],
                    ],
                ],
                'required_locales' => ['fr'],
            ]);
    }

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