<?php

namespace App\Form;

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

class UserType 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('isVerified', null, [
                'label' => 'L\'adresse email de l\'utilisateur a été vérifiée ?',
                'help' => 'Cochez si l\'adresse email est vérifiée. La connexion est impossible si l\'email n\'est pas vérifié.',
                'label_attr' => ['class' => 'checkbox'],
                'attr' => ['class' => 'checkbox'],
                '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('roles', ChoiceType::class, [
                'label' => 'Rôles',
                'help' => 'Sélectionnez les rôles de l\'utilisateur.',
                'choices' => [
                    'Participant' => 'ROLE_USER',
                    'Équipe' => 'ROLE_STAFF',
                    'Gestionnaire' => 'ROLE_MANAGER',
                    'Administrateur' => 'ROLE_ADMIN',
                ],
                'expanded' => true,
                'multiple' => true,
                'label_attr' => ['class' => 'label'],
                'attr' => ['class' => 'checkboxes'],
                'choice_attr' => ['class' => 'checkbox'],
                'help_attr' => ['class' => 'help'],
            ])
            ->add('newPassword', 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',
                    'help' => 'Vous mettez à jour un compte ? laissez vide pour ne pas changer',
                    '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('lastUpdate')
            ->add('lastLogin')
            ->add('lastAction') */
        ;
    }

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