<?php

namespace App\Form\User;

use App\Entity\Joueur;
use App\Entity\User;
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\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

class UserFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $isEdit = $options['data']?->getId() !== null;
        $builder
            ->add('pseudo', TextType::class ,[
                "label" => "Pseudo",
                "required" => true,
                "disabled" => $isEdit,
                "attr" => [
                    "placeholder" => "Pseudo",
                ]
            ])
            ->add('roles', ChoiceType::class, [
                "label" => "Roles",
                'choices' => [
                    'Admin' => 'ROLE_ADMIN',
                    'Joueur' => 'ROLE_JOUEUR',
                    'Visiteur' => 'ROLE_VISITEUR',
                ],
                'expanded' => true,
                'multiple' => true,
                'required' => false,
                'constraints' => [
                    new Callback(function (array $roles, ExecutionContextInterface $context) {
                        $mutuallyExclusive = ['ROLE_B', 'ROLE_C'];

                        // Intersection entre les rôles sélectionnés et les rôles exclusifs
                        $found = array_intersect($roles, $mutuallyExclusive);

                        if (count($found) > 1) {
                            $context
                                ->buildViolation('Les rôles B et C ne peuvent pas être attribués ensemble.')
                                ->addViolation();
                        }
                    }),
                ],
            ])
            ->add('password', PasswordType::class, [
                "label" => "Mot de passe",
                'mapped' => false,
                "required" => false,
                'attr' => [
                    'autocomplete' => 'new-password',
                    "placeholder" => "Mot de passe, laissez vide pour mot de passe par defaut",
                ]
            ])
            ->add('joueur', EntityType::class, [
                "label" => "Joueur associé",
                'class' => Joueur::class,
                'choice_label' => 'pseudo',
                "required" => false,
                "multiple" => false,
                "query_builder" => function (EntityRepository $er) use ($options) {
                    $qb = $er->createQueryBuilder('j')
                        ->leftJoin('j.owner', 'u')
                        ->where('u.id IS NULL');

                    // En mode édition, inclure le joueur déjà associé
                    $currentJoueur = $options['data']?->getJoueur();
                    if ($currentJoueur) {
                        $qb->orWhere('j.id = :current')
                            ->setParameter('current', $currentJoueur->getId());
                    }

                    return $qb->orderBy('j.pseudo', 'ASC');
                }
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => User::class,
        ]);
    }
}
