<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\EventBundle\Form;

use Chill\EventBundle\Entity\BudgetTypeEnum;
use Chill\EventBundle\Entity\EventBudgetElement;
use Chill\EventBundle\Entity\EventBudgetKind;
use Chill\EventBundle\Repository\EventBudgetKindRepository;
use Chill\MainBundle\Form\Type\CommentType;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class AddEventBudgetElementType extends AbstractType
{
    public function __construct(private readonly EventBudgetKindRepository $kindRepository, private readonly TranslatableStringHelperInterface $translatableStringHelper) {}

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $charges = $this->kindRepository->findByType(BudgetTypeEnum::CHARGE->value);
        $resources = $this->kindRepository->findByType(BudgetTypeEnum::RESOURCE->value);

        $builder->add('kind', ChoiceType::class, [
            'choices' => [
                'event.budget.charges' => $charges,
                'event.budget.resources' => $resources,
            ],
            'choice_label' => fn (EventBudgetKind $kind) => $this->translatableStringHelper->localize($kind->getName()),
            'choice_value' => fn (?EventBudgetKind $kind) => $kind?->getId(),
            'placeholder' => 'event.budget.Select a budget element kind',
        ])
            ->add('amount', NumberType::class, [
                'required' => true,
            ])
            ->add('comment', CommentType::class, [
                'label' => 'Comment',
                'required' => false,
            ]);
    }

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