<?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\Withdrawal;

use CpCreation\AltchaBundle\Form\AltchaType;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\Altcha\VitiCoreAltchaProtection;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Site\Withdrawal\Model\WithdrawalRequestData;
use CpCreation\VitiCore\Cart\Model\WithdrawalRequest;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class WithdrawalRequestType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('firstname', TextType::class, [
                'label' => 'withdrawal.form.firstname',
                'attr' => ['autocomplete' => 'given-name'],
            ])
            ->add('lastname', TextType::class, [
                'label' => 'withdrawal.form.lastname',
                'attr' => ['autocomplete' => 'family-name'],
            ])
            ->add('contractReference', TextType::class, [
                'label' => 'withdrawal.form.reference',
                'help' => 'withdrawal.form.reference_help',
                'attr' => ['autocomplete' => 'off'],
            ])
            ->add('receiptEmail', EmailType::class, [
                'label' => 'withdrawal.form.email',
                'help' => 'withdrawal.form.email_help',
                'attr' => ['autocomplete' => 'email'],
            ])
            ->add('orderReceived', ChoiceType::class, [
                'label' => 'withdrawal.form.order_received',
                'choices' => [
                    'withdrawal.form.yes' => true,
                    'withdrawal.form.no' => false,
                ],
                'expanded' => true,
                'multiple' => false,
                'placeholder' => false,
            ])
            ->add('receivedAt', DateType::class, [
                'label' => 'withdrawal.form.received_at',
                'help' => 'withdrawal.form.received_at_help',
                'widget' => 'single_text',
                'input' => 'datetime_immutable',
                'required' => false,
                'attr' => ['max' => new \DateTimeImmutable('today')->format('Y-m-d')],
            ])
            ->add('reasonCode', ChoiceType::class, [
                'label' => 'withdrawal.form.reason',
                'help' => 'withdrawal.form.reason_help',
                'required' => false,
                'placeholder' => 'withdrawal.form.reason_placeholder',
                'choices' => [
                    'withdrawal.reason.change_of_mind' => WithdrawalRequest::REASON_CHANGE_OF_MIND,
                    'withdrawal.reason.ordered_by_mistake' => WithdrawalRequest::REASON_ORDERED_BY_MISTAKE,
                    'withdrawal.reason.delivery_too_late' => WithdrawalRequest::REASON_DELIVERY_TOO_LATE,
                    'withdrawal.reason.product_not_as_expected' => WithdrawalRequest::REASON_PRODUCT_NOT_AS_EXPECTED,
                    'withdrawal.reason.other' => WithdrawalRequest::REASON_OTHER,
                ],
            ])
            ->add('comment', TextareaType::class, [
                'label' => 'withdrawal.form.comment',
                'help' => 'withdrawal.form.comment_help',
                'required' => false,
                'attr' => [
                    'rows' => 5,
                    'maxlength' => WithdrawalRequest::COMMENT_MAX_LENGTH,
                ],
            ]);

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

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