<?php

declare(strict_types=1);

namespace App\Form;

use App\Entity\AnneeScolaire;
use App\Entity\Classe;
use App\Entity\Ecole;
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\IntegerType;
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\Extension\Core\Type\TimeType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class ClasseType 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('nom', TextType::class, ['label' => 'Nom de la classe/groupe'])
            ->add('slug', TextType::class, ['label' => 'Slug', 'attr' => ['placeholder' => 'femmes-francais-whatsapp']])
            ->add('section', EntityType::class, ['class' => Section::class, 'choice_label' => 'nom', 'label' => 'Section', 'query_builder' => $filter])
            ->add('niveau', EntityType::class, ['class' => Niveau::class, 'choice_label' => 'nom', 'label' => 'Niveau', 'required' => false, 'placeholder' => 'Non défini', 'query_builder' => $filter])
            ->add('anneeScolaire', EntityType::class, ['class' => AnneeScolaire::class, 'choice_label' => 'libelle', 'label' => 'Année scolaire', 'required' => false, 'placeholder' => 'Non définie', 'query_builder' => fn (EntityRepository $er) => $er->createQueryBuilder('a')->andWhere('a.ecole = :ecole')->setParameter('ecole', $ecole)->orderBy('a.dateDebut', 'DESC')])
            ->add('langue', ChoiceType::class, ['label' => 'Langue', 'required' => false, 'placeholder' => 'Choisir', 'choices' => ['Français' => 'FRANCAIS', 'Haoussa' => 'HAOUSSA', 'Foulfouldé' => 'FOULFOULDE', 'Arabe' => 'ARABE']])
            ->add('sexe', ChoiceType::class, ['label' => 'Public', 'required' => false, 'placeholder' => 'Choisir', 'choices' => ['Hommes' => 'HOMME', 'Femmes' => 'FEMME', 'Mixte' => 'MIXTE']])
            ->add('mode', ChoiceType::class, ['label' => 'Mode', 'required' => false, 'placeholder' => 'Choisir', 'choices' => ['WhatsApp' => 'WHATSAPP', 'Présentiel' => 'PRESENTIEL', 'En ligne' => 'EN_LIGNE', 'Personnalisé' => 'PERSONNALISE']])
            ->add('capacite', IntegerType::class, ['label' => 'Capacité', 'required' => false, 'attr' => ['min' => 0]])
            ->add('joursCours', TextareaType::class, ['label' => 'Jours de cours', 'required' => false, 'attr' => ['rows' => 2, 'placeholder' => 'Ex : Lundi et jeudi']])
            ->add('heureDebut', TimeType::class, ['label' => 'Heure début', 'required' => false, 'widget' => 'single_text'])
            ->add('heureFin', TimeType::class, ['label' => 'Heure fin', 'required' => false, 'widget' => 'single_text'])
            ->add('tarifDefaut', MoneyType::class, ['label' => 'Tarif par défaut', 'required' => false, 'currency' => 'XAF', 'divisor' => 1])
            ->add('whatsappGroupId', TextType::class, [
                'label' => 'ID groupe WhatsApp',
                'required' => false,
                'help' => 'Optionnel. Prioritaire lors de la validation d’une inscription.',
            ])
            ->add('whatsappGroupInviteLink', TextType::class, [
                'label' => 'Lien d’invitation WhatsApp',
                'required' => false,
                'help' => 'Envoyé au candidat si l’ajout automatique échoue.',
            ])
            ->add('active', CheckboxType::class, ['label' => 'Classe active', 'required' => false]);
    }
    public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults(['data_class' => Classe::class, 'ecole' => null]); $resolver->setRequired('ecole'); $resolver->setAllowedTypes('ecole', [Ecole::class]); }
}
