<?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 UserProfileType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('firstName', null, [
                'label' => 'Prénom',
                'required' => true,
                'label_attr' => ['class' => 'label'],
                'attr' => ['class' => 'input'],
                'row_attr' => ['class' => 'field'],
                'help_attr' => ['class' => 'help'],
            ])
            ->add('lastName', null, [
                'label' => 'Nom',
                'required' => true,
                '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('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',
                    '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,
        ]);
    }
}
