<?php

declare(strict_types=1);

namespace App\Form;

use App\Entity\Classe;
use App\Entity\Ecole;
use App\Entity\Etudiant;
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\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
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;

final class EtudiantType 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.nom', 'ASC');

        $builder
            ->add('matricule', TextType::class, ['label' => 'Matricule', 'required' => false, 'help' => 'Laissez vide pour générer automatiquement à la création.'])
            ->add('nom', TextType::class, ['label' => 'Noms'])
            ->add('sexe', ChoiceType::class, ['label' => 'Sexe', 'required' => false, 'placeholder' => 'Choisir', 'choices' => ['Homme' => 'HOMME', 'Femme' => 'FEMME']])
            ->add('dateNaissance', DateType::class, ['label' => 'Date de naissance', 'required' => false, 'widget' => 'single_text'])
            ->add('lieuNaissance', TextType::class, ['label' => 'Lieu de naissance', 'required' => false])
            ->add('situationMatrimoniale', ChoiceType::class, ['label' => 'Situation matrimoniale', 'required' => false, 'placeholder' => 'Choisir', 'choices' => ['Célibataire' => 'CELIBATAIRE', 'Marié(e)' => 'MARIE', 'Autre' => 'AUTRE']])
            ->add('aDesEnfants', CheckboxType::class, ['label' => 'A des enfants', 'required' => false])
            ->add('telephonePrincipal', TextType::class, ['label' => 'Téléphone principal', 'required' => false])
            ->add('telephoneWhatsapp', TextType::class, ['label' => 'WhatsApp', 'required' => false])
            ->add('autreContact', TextType::class, ['label' => 'Autre contact', 'required' => false])
            ->add('email', EmailType::class, ['label' => 'Email', 'required' => false])
            ->add('adresse', TextareaType::class, ['label' => 'Adresse de résidence', 'required' => false, 'attr' => ['rows' => 2]])
            ->add('joursDisponibles', TextareaType::class, ['label' => 'Jours disponibles', 'required' => false, 'attr' => ['rows' => 2]])
            ->add('plageHoraireSouhaitee', TextType::class, ['label' => 'Plage horaire souhaitée', 'required' => false])
            ->add('section', EntityType::class, ['class' => Section::class, 'choice_label' => 'nom', 'label' => 'Section', 'required' => false, 'placeholder' => 'Choisir', 'query_builder' => $filter])
            ->add('niveau', EntityType::class, ['class' => Niveau::class, 'choice_label' => 'nom', 'label' => 'Niveau', 'required' => false, 'placeholder' => 'Choisir', 'query_builder' => $filter])
            ->add('classe', EntityType::class, ['class' => Classe::class, 'choice_label' => 'nom', 'label' => 'Classe', 'required' => false, 'placeholder' => 'Choisir', 'query_builder' => $filter])
            ->add('ancien', CheckboxType::class, ['label' => 'Ancien apprenant', 'required' => false])
            ->add('actif', CheckboxType::class, ['label' => 'Étudiant actif', 'required' => false]);
    }

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