<?php

namespace KupShop\B2BBundle\Twig\Components\B2BUserRegistration;

use KupShop\KupShopBundle\Context\CountryContext;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Contracts\Service\Attribute\Required;

class FormType extends AbstractType
{
    #[Required]
    public CountryContext $countryContext;

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', TextType::class, [
                'label' => 'form.label.name',
                'required' => true,
            ])
            ->add('surname', TextType::class, [
                'label' => 'form.label.surname',
                'required' => true,
            ])
            ->add('email', EmailType::class, [
                'label' => 'form.label.email',
                'required' => true,
            ])
            ->add('invoice_email', EmailType::class, [
                'label' => 'form.label.invoice_email',
                'required' => false,
            ])
            ->add('company', TextType::class, [
                'label' => 'form.label.company',
                'required' => true,
            ])
            ->add('phone', TextType::class, [
                'label' => 'form.label.phone',
                'constraints' => [
                    new Regex([
                        'pattern' => '/^\+?[0-9]{9,15}$/',
                        'message' => 'form.label.phone.invalid',
                    ]),
                ],
                'required' => true,
            ])
            ->add('ico', TextType::class, [
                'label' => 'form.label.ico',
                'required' => true,
            ])
            ->add('dic', TextType::class, [
                'label' => 'form.label.dic',
                'required' => true,
            ])
            ->add('street', TextType::class, [
                'label' => 'form.label.street',
                'required' => true,
            ])
            ->add('city', TextType::class, [
                'label' => 'form.label.city',
                'required' => true,
            ])
            ->add('psc', NumberType::class, [
                'label' => 'form.label.psc',
                'required' => true,
            ])
            ->add('country', ChoiceType::class, [
                'label' => 'form.label.country',
                'required' => true,
                'preferred_choices' => [$this->countryContext->getActive()->getName()],
                'duplicate_preferred_choices' => false,
                'choice_loader' => new CallbackChoiceLoader(function (): array {
                    $countries = array_map(function ($e) {return $e->getName(); }, $this->countryContext->getAll());

                    return array_combine($countries, $countries);
                }),
            ])
            ->add('additional_address', TextType::class, [
                'label' => 'form.label.additional_address',
                'required' => false,
            ])
            ->add('iname', TextType::class, [
                'label' => 'form.label.name',
                'required' => true,
            ])
            ->add('isurname', TextType::class, [
                'label' => 'form.label.surname',
                'required' => true,
            ])
            ->add('istreet', TextType::class, [
                'label' => 'form.label.street',
                'required' => true,
            ])
            ->add('additional_address_delivery', CheckboxType::class, [
                'label' => false,
                'required' => false,
            ])
            ->add('icity', TextType::class, [
                'label' => 'form.label.city',
                'required' => true,
            ])
            ->add('ipsc', NumberType::class, [
                'label' => 'form.label.psc',
                'required' => true,
            ]);
    }

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