<?php

declare(strict_types=1);

namespace App\Form\Finance;

use App\Entity\AnneeScolaire;
use App\Entity\Classe;
use App\Entity\Ecole;
use App\Entity\RegleFrais;
use App\Entity\Section;
use App\Entity\TypeFrais;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class RegleFraisType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        /** @var Ecole $ecole */
        $ecole = $options['ecole'];
        $filter = fn (EntityRepository $er) => $er->createQueryBuilder('e')
            ->andWhere('e.ecole = :ecole')
            ->setParameter('ecole', $ecole)
            ->orderBy('e.id', 'DESC');
        $nameFilter = fn (EntityRepository $er) => $er->createQueryBuilder('e')
            ->andWhere('e.ecole = :ecole')
            ->setParameter('ecole', $ecole)
            ->orderBy('e.nom', 'ASC');
        $typeFilter = fn (EntityRepository $er) => $er->createQueryBuilder('e')
            ->andWhere('e.ecole = :ecole')
            ->setParameter('ecole', $ecole)
            ->orderBy('e.libelle', 'ASC');

        $builder
            ->add('typeFrais', EntityType::class, ['class' => TypeFrais::class, 'choice_label' => 'libelle', 'label' => 'Type de frais', 'placeholder' => 'Choisir', 'query_builder' => $typeFilter])
            ->add('anneeScolaire', EntityType::class, ['class' => AnneeScolaire::class, 'choice_label' => 'libelle', 'label' => 'Année scolaire', 'required' => false, 'placeholder' => 'Toutes les années', 'query_builder' => $filter])
            ->add('section', EntityType::class, ['class' => Section::class, 'choice_label' => 'nom', 'label' => 'Section', 'required' => false, 'placeholder' => 'Toutes les sections', 'query_builder' => $nameFilter])
            ->add('classe', EntityType::class, ['class' => Classe::class, 'choice_label' => 'nom', 'label' => 'Classe', 'required' => false, 'placeholder' => 'Toutes les classes', 'query_builder' => $nameFilter])
            ->add('montant', MoneyType::class, ['label' => 'Montant', 'currency' => 'XAF', 'divisor' => 1])
            ->add('periodicite', ChoiceType::class, [
                'label' => 'Périodicité',
                'required' => false,
                'placeholder' => 'Utiliser celle du type',
                'choices' => [
                    'Unique' => TypeFrais::PERIODICITE_UNIQUE,
                    'Annuelle' => TypeFrais::PERIODICITE_ANNUELLE,
                    'Mensuelle' => TypeFrais::PERIODICITE_MENSUELLE,
                    'Personnalisée' => TypeFrais::PERIODICITE_PERSONNALISEE,
                ],
            ])
            ->add('moisDebut', IntegerType::class, ['label' => 'Mois de début', 'required' => false, 'attr' => ['min' => 1, 'max' => 12]])
            ->add('nombreEcheances', IntegerType::class, ['label' => 'Nombre d’échéances', 'required' => false, 'attr' => ['min' => 1]])
            ->add('description', TextareaType::class, ['label' => 'Description', 'required' => false, 'attr' => ['rows' => 3]])
            ->add('actif', CheckboxType::class, ['label' => 'Actif', 'required' => false]);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults(['data_class' => RegleFrais::class, 'ecole' => null]);
        $resolver->setRequired('ecole');
        $resolver->setAllowedTypes('ecole', [Ecole::class]);
    }
}
