<?php

namespace App\Form;

use App\Entity\User;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;

class RegistrationFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('firstName', null, [
                'label' => 'Prénom',
                'required' => true,
                'help' => 'Entrez le prénom de l\'utilisateur.',
                'label_attr' => ['class' => 'label'],
                'attr' => ['class' => 'input'],
                'row_attr' => ['class' => 'field'],
                'help_attr' => ['class' => 'help'],
            ])
            ->add('lastName', null, [
                'label' => 'Nom',
                'required' => true,
                'help' => 'Entrez le nom de l\'utilisateur.',
                'label_attr' => ['class' => 'label'],
                'attr' => ['class' => 'input'],
                'row_attr' => ['class' => 'field'],
                'help_attr' => ['class' => 'help'],
            ])
            ->add('email', null, [
                'label' => 'email',
                'required' => true,
                'help' => 'Entrez une adresse email.',
                'label_attr' => ['class' => 'label'],
                'attr' => ['class' => 'input'],
                'row_attr' => ['class' => 'field'],
                'help_attr' => ['class' => 'help'],
            ])
            ->add('phone', null, [
                'label' => 'Numéro de téléphone mobile',
                'required' => false,
                'help' => 'Entrez un numéro de téléphone mobile (optionnel).',
                'label_attr' => ['class' => 'label'],
                'attr' => ['class' => 'input'],
                'row_attr' => ['class' => 'field'],
                'help_attr' => ['class' => 'help'],
            ])
            ->add('plainPassword', RepeatedType::class, [
                'mapped' => false,
                'required' => false,
                'invalid_message' => 'Les mots de passe ne sont pas identiques.',
                'type' => PasswordType::class,
                'first_options' => [
                    'label' => 'Mot de passe',
                    'toggle' => true,
                    'attr' => ['autocomplete' => 'new-password', 'class' => 'input'],
                    'label_attr' => ['class' => 'label'],
                    'row_attr' => ['class' => 'field'],
                    'help_attr' => ['class' => 'help'],
                    'constraints' => [
                        new Length([
                            'min' => 6,
                            'minMessage' => 'Votre mot de passe doit contenir au moins {{ limit }} caractères',
                            // max length allowed by Symfony for security reasons
                            'max' => 4096,
                        ]),
                    ]],
                'second_options' => [
                    'label' => 'Répétez le mot de passe',
                    'toggle' => true,
                    'attr' => ['autocomplete' => 'new-password', 'class' => 'input'],
                    'label_attr' => ['class' => 'label'],
                    'row_attr' => ['class' => 'field'],
                    'help_attr' => ['class' => 'help'],
                    ],
            ])
            ->add('agreeTerms', CheckboxType::class, [
                'label' => 'J\'accepte les conditions d\'utilisation du service.',
                'mapped' => false,
                'constraints' => [
                    new IsTrue([
                        'message' => 'Vous devez accepter les conditions d\'utilisation du service.',
                    ]),
                ],
                'label_attr' => ['class' => 'checkbox'],
                'attr' => ['class' => 'checkbox'],
                'row_attr' => ['class' => 'field'],
                'help_attr' => ['class' => 'help'],
            ])
/*             ->add('plainPassword', PasswordType::class, [
                // instead of being set onto the object directly,
                // this is read and encoded in the controller
                'mapped' => false,
                'toggle' => true,
                'attr' => ['autocomplete' => 'new-password'],
                'constraints' => [
                    new NotBlank([
                        'message' => 'Please enter a password',
                    ]),
                    new Length([
                        'min' => 6,
                        'minMessage' => 'Your password should be at least {{ limit }} characters',
                        // max length allowed by Symfony for security reasons
                        'max' => 4096,
                    ]),
                ],
            ]) */

        ;
    }

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