<?php

declare(strict_types=1);

namespace App\Form\Finance;

use App\Entity\AnneeScolaire;
use App\Entity\CategorieDepense;
use App\Entity\Depense;
use App\Entity\Ecole;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
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;
use Symfony\Component\Validator\Constraints\GreaterThan;

final class DepenseType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        /** @var Ecole $ecole */
        $ecole = $options['ecole'];

        $builder
            ->add('anneeScolaire', EntityType::class, [
                'class' => AnneeScolaire::class,
                'choice_label' => 'libelle',
                'label' => 'Année scolaire',
                'required' => false,
                'placeholder' => 'Non liée à une année',
                'query_builder' => fn (EntityRepository $er) => $er->createQueryBuilder('a')
                    ->andWhere('a.ecole = :ecole')
                    ->setParameter('ecole', $ecole)
                    ->orderBy('a.dateDebut', 'DESC')
                    ->addOrderBy('a.id', 'DESC'),
            ])
            ->add('categorie', EntityType::class, [
                'class' => CategorieDepense::class,
                'choice_label' => 'libelle',
                'label' => 'Catégorie',
                'placeholder' => 'Choisir une catégorie',
                'query_builder' => fn (EntityRepository $er) => $er->createQueryBuilder('c')
                    ->andWhere('c.ecole = :ecole')
                    ->setParameter('ecole', $ecole)
                    ->orderBy('c.ordre', 'ASC')
                    ->addOrderBy('c.libelle', 'ASC'),
            ])
            ->add('montant', MoneyType::class, [
                'label' => 'Montant',
                'currency' => 'XAF',
                'divisor' => 1,
                'constraints' => [new GreaterThan(['value' => 0, 'message' => 'Le montant doit être supérieur à 0.'])],
            ])
            ->add('dateDepense', DateType::class, [
                'label' => 'Date de dépense',
                'widget' => 'single_text',
            ])
            ->add('beneficiaire', TextType::class, [
                'label' => 'Bénéficiaire',
                'required' => false,
            ])
            ->add('description', TextareaType::class, [
                'label' => 'Description',
                'required' => false,
                'attr' => ['rows' => 3],
            ])
            ->add('modePaiement', ChoiceType::class, [
                'label' => 'Mode de paiement',
                'required' => false,
                'placeholder' => 'Choisir',
                'choices' => [
                    'Espèces' => 'ESPECES',
                    'Orange Money' => 'ORANGE_MONEY',
                    'MTN MoMo' => 'MTN_MOMO',
                    'Mobile Money' => 'MOBILE_MONEY',
                    'Virement' => 'VIREMENT',
                    'Autre' => 'AUTRE',
                ],
            ])
            ->add('reference', TextType::class, [
                'label' => 'Référence',
                'required' => false,
            ])
            ->add('justificatifPath', TextType::class, [
                'label' => 'Référence justificatif',
                'required' => false,
            ]);
    }

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