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

use CpCreation\VitiCore\AgeVerification\Model\AgeVerificationConfig;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class AgeVerificationConfigType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('enabled', CheckboxType::class, [
                'label' => 'age_verification.admin.enabled',
                'help' => 'age_verification.admin.enabled_help',
                'required' => false,
            ])
            ->add('defaultCountryCode', CountryType::class, [
                'label' => 'age_verification.admin.default_country',
                'help' => 'age_verification.admin.default_country_help',
                'attr' => ['class' => 'select2'],
            ])
            ->add('defaultMinimumAge', IntegerType::class, [
                'label' => 'age_verification.admin.default_minimum_age',
                'help' => 'age_verification.admin.default_minimum_age_help',
                'attr' => [
                    'min' => 1,
                    'max' => 99,
                ],
            ])
            ->add('rememberForDays', IntegerType::class, [
                'label' => 'age_verification.admin.remember_for_days',
                'help' => 'age_verification.admin.remember_for_days_help',
                'attr' => [
                    'min' => 1,
                    'max' => 365,
                ],
            ])
            ->add('countryRules', CollectionType::class, [
                'label' => 'age_verification.admin.country_rules',
                'help' => 'age_verification.admin.country_rules_help',
                'entry_type' => AgeVerificationCountryRuleType::class,
                'entry_options' => ['label' => false],
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false,
                'prototype' => true,
                'required' => false,
                'attr' => [
                    'class' => 'age-verification-country-rule-collection',
                ],
            ]);
    }

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