<?php

declare(strict_types=1);

namespace App\Form;

use App\Entity\Classe;
use App\Entity\Ecole;
use App\Entity\Inscription;
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\CollectionType;
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 PublicInscriptionType 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('typeCandidat', ChoiceType::class, [
                'label' => 'Votre situation',
                'choices' => ['Je suis un(e) nouvel(le) étudiant(e)' => Inscription::TYPE_NOUVEAU, 'Je suis un(e) ancien(ne) étudiant(e)' => Inscription::TYPE_ANCIEN],
                'expanded' => true,
            ])
            ->add('ancienMatricule', TextType::class, ['label' => 'Ancien matricule / matricule existant', 'required' => false])
            ->add('nom', TextType::class, ['label' => 'Noms et prénoms complets'])
            ->add('sexe', ChoiceType::class, ['label' => 'Sexe de l’apprenant', '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 (ville/pays)', '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' => 'Avez-vous des enfants ?', 'required' => false])
            ->add('telephonePrincipal', TextType::class, ['label' => 'Téléphone principal de contact', 'required' => false])
            ->add('telephoneWhatsapp', TextType::class, ['label' => 'Numéro WhatsApp à contacter', 'required' => false])
            ->add('autreContact', TextType::class, ['label' => 'Autre numéro ou contact utile', 'required' => false])
            ->add('email', EmailType::class, ['label' => 'Adresse email (facultatif)', 'required' => false])
            ->add('adresse', TextareaType::class, ['label' => 'Adresse de résidence ou quartier', 'required' => false, 'attr' => ['rows' => 2]])
            ->add('sectionSouhaitee', EntityType::class, ['class' => Section::class, 'choice_label' => 'nom', 'label' => 'Programme / 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 connu', 'required' => false, 'placeholder' => 'Choisir', 'query_builder' => $filter])
            ->add('langueSouhaitee', ChoiceType::class, ['label' => 'Langue d’apprentissage 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('parcours', CollectionType::class, [
                'entry_type' => PublicInscriptionParcoursType::class,
                'entry_options' => ['ecole' => $ecole],
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false,
                'delete_empty' => static fn ($parcours = null): bool => $parcours === null
                    || (!$parcours->getSectionSouhaitee() && !$parcours->getNiveauSouhaite() && !$parcours->getClasseSouhaitee() && !$parcours->getLangueSouhaitee() && !$parcours->getModeSouhaite() && !$parcours->getObservation()),
                'label' => false,
            ])
            ->add('joursDisponibles', TextareaType::class, ['label' => 'Jours où vous êtes disponible', 'required' => false, 'attr' => ['rows' => 2, 'placeholder' => 'Ex : lundi, mercredi, samedi']])
            ->add('plageHoraireSouhaitee', TextType::class, ['label' => 'Horaire ou période souhaitée', 'required' => false, 'attr' => ['placeholder' => 'Ex : soir après 20h, matin, week-end']])
            ->add('observationCandidat', TextareaType::class, ['label' => 'Message complémentaire pour l’administration', 'required' => false, 'attr' => ['rows' => 4]]);
    }

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