<?php

namespace App\Form;

use App\Entity\Snip;
use App\Service\SnipParser\ParserFactory;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
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 SnipType extends AbstractType
{
    public function __construct(
        private readonly ParserFactory $parserFactory,
    ) {}

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name')
            ->add('parser', ChoiceType::class, [
                'choice_label' => fn(string $parser) => ucfirst($parser),
                'choices' => $this->parserFactory->getChoices(),
            ])
            ->add('content', TextareaType::class, [
                'attr' => ['rows' => 20],
                'mapped' => false,
            ])
            ->add('tags', TagsType::class)
            ->add('public', SwitchType::class)
            ->add('visible', SwitchType::class)
            ->add('contentName', TextType::class, [
                'label' => 'Change description (optional)',
                'mapped' => false,
                'required' => false,
            ])
        ;
    }

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