<?php

namespace App\Controller\Admin\EventConfig;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Requirement\Requirement;
use Symfony\Component\Routing\Attribute\Route;
use Doctrine\ORM\EntityManagerInterface;

use App\Entity\Event;
use App\Entity\Gamemaster;
use App\Repository\GamemasterRepository;

final class GamemasterController extends AbstractController
{
    #[Route('/admin/event/{id}/configure/gamemaster', name: 'app_admin_event_config_gamemaster', requirements: ['id' => Requirement::UUID_V7], methods: ['GET', 'POST'])]
    public function index(?Event $event, GamemasterRepository $repository): Response
    {
        $gamemasters = $repository->findAll();
        return $this->render('admin/event/config/gamemaster.html.twig', [
            'gamemasters' => $gamemasters,
            'event' => $event,
        ]);
    }

    #[Route('/admin/event/{id}/configure/gamemaster/toggle/', name: 'app_admin_event_config_gamemaster_toggle', requirements: ['id' => Requirement::UUID_V7], methods: ['POST'])]
    public function toggle(?Event $event, Request $request, GamemasterRepository $repository, EntityManagerInterface $entityManager): JsonResponse
    {
        // Récupération du MJ
        $data = json_decode($request->getContent(), true);
        $gamemasterID = $data['gamemasterId'] ?? null;
        if (!$gamemasterID) {
            return new JsonResponse(['success' => false]);
        }
        $gamemaster = $repository->findByStrID($gamemasterID);
        
        if (in_array($gamemaster, $event->getGamemastersAssigned()->toArray()))
        {
            // Enlever le MJ de la liste
            $event->removeGamemastersAssigned($gamemaster);
            // Enlever les jeux spécifiques à ce MJ des jeux de l'événement
            foreach($gamemaster->getGamesCanMaster() as $game) {
                $notFound = true;
                foreach($event->getGamemastersAssigned() as $otherGM) {
                    if (in_array($game, $otherGM->getGamesCanMaster()->toArray())) {
                        $notFound = false;
                    }
                }
                if ($notFound) {
                    $event->removeGameAssigned($game);
                }
            }

            // marquer la réponse
            $state='DISABLE';

        } else {
            // Ajouter le MJ à la liste
            $event->addGamemastersAssigned($gamemaster);
            // Ajouter les jeux spécifiques à ce MJ dans les jeux de l'événement
            // Désactivé le 08/08/2025 -> géré dans JS jeux
            /*foreach($gamemaster->getGamesCanMaster() as $game) {
                if (!in_array($game, $event->getGameAssigned()->toArray())) {
                    $event->addGameAssigned($game);
                }
            }*/
            // marquer la réponse
            $state='ENABLE';

        }
        $entityManager->persist($event);
        $entityManager->flush();

        return new JsonResponse(['success' => true, 'status' => $state]);

    }

}
