<?php

/*
 * 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\Site\Security;

use CpCreation\AltchaBundle\Form\AltchaType;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\Altcha\VitiCoreAltchaProtection;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Site\Security\Model\ResetPasswordRequest;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class ResetPasswordType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('plainPassword', PasswordType::class, [
                'label' => 'security.new_password.label',
                'always_empty' => false,
                'attr' => [
                    'autocomplete' => 'new-password',
                ],
            ])
            ->add('passwordConfirmation', PasswordType::class, [
                'label' => 'security.password_confirmation.label',
                'always_empty' => false,
                'attr' => [
                    'autocomplete' => 'new-password',
                ],
            ]);

        if ($options['include_captcha']) {
            $builder->add('captcha', AltchaType::class, [
                'scope' => VitiCoreAltchaProtection::scopeFor(self::class),
                'partial_live_actions' => VitiCoreAltchaProtection::partialLiveActionsFor(self::class),
                'final_live_actions' => VitiCoreAltchaProtection::finalLiveActionsFor(self::class),
            ]);
        }
    }

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

        $resolver->setAllowedTypes('include_captcha', 'bool');
    }
}
