<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CmsBundle\Form\CompoundType;

use Ferienpass\CoreBundle\Entity\PostalAddress;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Regex;

class PostalAddressType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('street', TextType::class, [
                'label' => 'user.street',
                'constraints' => [
                    new Regex(pattern: '/\d/', message: 'Bitte geben Sie die Straße inklusive Hausnummer an.'),
                ],
            ])
            ->add('postalCode', TextType::class, [
                'label' => 'user.postalCode',
            ])
            ->add('city', TextType::class, [
                'label' => 'user.city',
            ])
            ->add('country', CountryType::class, [
                'label' => 'user.country',
                'preferred_choices' => ['DE'],
            ])
        ;
    }

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