<?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\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Form\StoredObjectType;
use Chill\EventBundle\Entity\BudgetTypeEnum;
use Chill\EventBundle\Entity\Event;
use Chill\EventBundle\Form\Type\PickEventThemeType;
use Chill\EventBundle\Form\Type\PickEventTypeType;
use Chill\EventBundle\Repository\EventBudgetKindRepository;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Form\DataTransformer\IdToLocationDataTransformer;
use Chill\MainBundle\Form\Type\ChillCollectionType;
use Chill\MainBundle\Form\Type\ChillDateTimeType;
use Chill\MainBundle\Form\Type\CommentType;
use Chill\MainBundle\Form\Type\PickUserDynamicType;
use Chill\MainBundle\Form\Type\ScopePickerType;
use Chill\ThirdPartyBundle\Form\Type\PickThirdpartyDynamicType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;

class EventType extends AbstractType
{
    public function __construct(
        private readonly IdToLocationDataTransformer $idToLocationDataTransformer,
        private readonly EventBudgetKindRepository $eventBudgetKindRepository,
        private readonly TranslatorInterface $translator,
    ) {}

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

        $builder
            ->add('name', TextType::class, [
                'required' => true,
            ])
            ->add('date', ChillDateTimeType::class, [
                'required' => true,
            ])
            ->add('circle', ScopePickerType::class, [
                'center' => $options['center'],
                'role' => $options['role'],
            ])
            ->add('type', PickEventTypeType::class, [
                'placeholder' => 'Pick a type of event',
                'attr' => [
                    'class' => '',
                ],
            ])
            ->add('themes', PickEventThemeType::class, [
                'multiple' => true,
            ])
            ->add('moderator', PickUserDynamicType::class, [
                'label' => 'Pick a moderator',
            ])
            ->add('animatorsIntern', PickUserDynamicType::class, [
                'multiple' => true,
                'label' => $this->translator->trans('event.fields.internal animators'),
                'required' => false,
            ])
            ->add('animatorsExtern', PickThirdpartyDynamicType::class, [
                'multiple' => true,
                'label' => $this->translator->trans('event.fields.external animators'),
                'required' => false,
            ])
            ->add('budgetElements', ChillCollectionType::class, [
                'entry_type' => AddEventBudgetElementType::class,
                'entry_options' => ['label' => false],
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false,
            ])
            ->add('comment', CommentType::class, [
                'label' => 'Comment',
                'required' => false,
            ])
            ->add('documents', ChillCollectionType::class, [
                'entry_type' => StoredObjectType::class,
                'entry_options' => [
                    'has_title' => true,
                ],
                'allow_add' => true,
                'allow_delete' => true,
                'delete_empty' => fn (StoredObject $storedObject): bool => '' === $storedObject->getFilename(),
                'button_remove_label' => 'event.form.remove_document',
                'button_add_label' => 'event.form.add_document',
            ]);

        $builder->add('location', HiddenType::class)
            ->get('location')
            ->addModelTransformer($this->idToLocationDataTransformer);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Event::class,
        ]);
        $resolver
            ->setRequired(['center', 'role'])
            ->setAllowedTypes('center', Center::class)
            ->setAllowedTypes('role', 'string');
    }

    public function getBlockPrefix(): string
    {
        // as the js shares some hardcoded items from the activity bundle, we have to rewrite block prefix
        return 'chill_activitybundle_activity';
    }
}
