<?php

declare(strict_types=1);

namespace App\Form;

use App\Entity\Classe;
use App\Entity\Ecole;
use App\Entity\InscriptionParcours;
use App\Entity\Niveau;
use App\Entity\Section;
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\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class PublicInscriptionParcoursType 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')
            ->andWhere('e.active = :active')
            ->setParameter('ecole', $ecole)
            ->setParameter('active', true)
            ->orderBy('e.nom', 'ASC');

        $builder
            ->add('sectionSouhaitee', EntityType::class, ['class' => Section::class, 'choice_label' => 'nom', 'label' => 'Programme ou section souhaité(e)', 'required' => false, 'placeholder' => 'Choisir', 'query_builder' => $filter])
            ->add('niveauSouhaite', EntityType::class, ['class' => Niveau::class, 'choice_label' => 'nom', 'label' => 'Niveau souhaité ou niveau estimé', 'required' => false, 'placeholder' => 'Choisir', 'query_builder' => $filter])
            ->add('classeSouhaitee', EntityType::class, ['class' => Classe::class, 'choice_label' => 'nom', 'label' => 'Classe ou groupe souhaité si vous le connaissez', 'required' => false, 'placeholder' => 'Choisir', 'query_builder' => $filter])
            ->add('langueSouhaitee', ChoiceType::class, ['label' => 'Langue souhaitée', 'required' => false, 'placeholder' => 'Choisir', 'choices' => ['Français' => 'FRANCAIS', 'Haoussa' => 'HAOUSSA', 'Foulfouldé' => 'FOULFOULDE', 'Arabe' => 'ARABE']])
            ->add('modeSouhaite', ChoiceType::class, ['label' => 'Mode de cours souhaité', 'required' => false, 'placeholder' => 'Choisir', 'choices' => ['WhatsApp' => 'WHATSAPP', 'Présentiel' => 'PRESENTIEL', 'En ligne' => 'EN_LIGNE', 'Personnalisé' => 'PERSONNALISE']])
            ->add('observation', TextareaType::class, ['label' => 'Observation courte sur ce programme', 'required' => false, 'attr' => ['rows' => 2]]);
    }

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