<?php

/*
 * This file is part of the Serenity 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\Order;

use CpCreation\VitiCore\Cart\Model\InvoiceAddress;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Intl\Countries;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class InvoiceAddressType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('company', TextType::class, [
                'label' => 'shop.form.company',
                'attr' => [
                    'placeholder' => 'shop.form.company',
                    'autocomplete' => 'billing organization',
                ],
                'required' => false,
            ])
            ->add('siret', TextType::class, [
                'label' => 'shop.form.siret',
                'attr' => [
                    'placeholder' => 'shop.form.siret',
                    'inputmode' => 'numeric',
                    'maxlength' => 14,
                ],
                'required' => false,
            ])
            ->add('vatId', TextType::class, [
                'label' => 'shop.form.tva',
                'attr' => [
                    'placeholder' => 'shop.form.tva',
                    'autocomplete' => 'billing organization-title',
                ],
                'required' => false,
            ])
            ->add('lastname', TextType::class, [
                'label' => 'shop.form.lastname',
                'attr' => [
                    'placeholder' => 'shop.form.lastname',
                    'autocomplete' => 'billing family-name',
                ],
                'required' => true,
            ])
            ->add('firstname', TextType::class, [
                'label' => 'shop.form.firstname',
                'attr' => [
                    'placeholder' => 'shop.form.firstname',
                    'autocomplete' => 'billing given-name',
                ],
                'required' => true,
            ])
            ->add('street', TextType::class, [
                'label' => 'shop.form.address',
                'attr' => [
                    'placeholder' => 'shop.form.address',
                    'autocomplete' => 'billing street-address',
                ],
                'required' => true,
            ])
            ->add('postal', TextType::class, [
                'label' => 'shop.form.postal_code',
                'attr' => [
                    'placeholder' => 'shop.form.postal_code',
                    'autocomplete' => 'billing postal-code',
                ],
                'required' => true,
            ])
            ->add('city', TextType::class, [
                'label' => 'shop.form.city',
                'attr' => [
                    'placeholder' => 'shop.form.city',
                    'autocomplete' => 'billing address-level2',
                ],
                'required' => true,
            ])
            ->add('email', EmailType::class, [
                'label' => 'shop.form.billing_email',
                'attr' => [
                    'placeholder' => 'shop.form.email',
                    'autocomplete' => 'billing email',
                ],
                'required' => true,
            ])

            // champ technique (remplacé dynamiquement)
            ->add('country', ChoiceType::class, [
                'label' => 'shop.form.country',
                'required' => true,
                'choices' => array_flip(Countries::getNames()),
                'choice_label' => static fn (?string $v): string => $v ? Countries::getName($v) : '',
                'attr' => [
                    'separator' => '=====',
                    'autocomplete' => 'billing country',
                ],
            ])
            ->add('phone', TelType::class, [
                'label' => 'shop.form.phone',
                'attr' => [
                    'placeholder' => 'shop.form.phone',
                    'autocomplete' => 'billing tel',
                ],
                'required' => true,
            ])
            ->add('differentAddress', CheckboxType::class, [
                'label' => 'shop.different_address_text',
                'required' => false,
            ]);

        $builder->addEventListener(FormEvents::PRE_SET_DATA, [$this, 'onPreSetData']);
    }

    public function onPreSetData(FormEvent $event): void
    {
        $form = $event->getForm();
        $data = $event->getData(); // InvoiceAddress|null

        $choices = array_flip(Countries::getNames());

        $default = 'FR';
        if ($data instanceof InvoiceAddress && $data->getCountry()) {
            $default = (string) $data->getCountry();
        }

        if (!\in_array($default, $choices, true)) {
            $default = 'FR';
        }

        if ($form->has('country')) {
            $form->remove('country');
        }

        $form->add('country', ChoiceType::class, [
            'label' => 'shop.form.country',
            'required' => true,
            'choices' => $choices,
            'data' => $default,
            'choice_label' => static fn (?string $v): string => $v ? Countries::getName($v) : '',
            'attr' => [
                'separator' => '=====',
                'autocomplete' => 'billing country',
            ],
        ]);
    }

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