<?php

namespace KupShop\ComponentsBundle\Twig\Components\Utils\Forms\DSA;

use KupShop\KupShopBundle\Context\UserContext;
use Symfony\Component\Form\AbstractType;
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;

class FormType extends AbstractType
{
    public const NOT_LOGGED_GROUP = 'not_logged';
    public const NO_REPORT_TYPE = 'no_report_type';

    public function __construct(private readonly UserContext $userContext)
    {
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $notLogged = in_array(self::NOT_LOGGED_GROUP, $options['validation_groups'] ?? []);
        $noReportType = in_array(self::NO_REPORT_TYPE, $options['validation_groups'] ?? []);

        $builder
            ->add('name', TextType::class, [
                'label' => 'form.label.name',
                'required' => false,
            ])
            ->add('email', EmailType::class, [
                'label' => 'form.label.email',
                'required' => $notLogged,
            ])
            ->add('page_url', TextType::class, [
                'label' => 'form.label.dsa_page_url',
                'required' => $noReportType,
            ])
            ->add('report_message', TextareaType::class, [
                'label' => 'form.label.dsa_report_message',
                'required' => true,
            ]);
    }

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