<?php

declare(strict_types=1);

namespace App\Controller\School;

use App\Service\School\SchoolContext;
use App\Entity\Classe;
use App\Entity\Ecole;
use App\Form\ClasseType;
use App\Repository\ClasseRepository;
use App\Repository\EtudiantRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;

#[Route('/school/classes', name: 'school_classe_')]
#[IsGranted('ROLE_ECOLE_ADMIN')]
final class ClasseController extends AbstractController
{
    public function __construct(private readonly SchoolContext $schoolContext)
    {
    }

    #[Route('', name: 'index', methods: ['GET'])]
    public function index(ClasseRepository $repository): Response
    {
        $ecole = $this->schoolContext->getEcole();
        return $this->render('school/classe/index.html.twig', ['ecole' => $ecole, 'classes' => $repository->findForEcole($ecole)]);
    }

    #[Route('/new', name: 'new', methods: ['GET', 'POST'])]
    public function new(Request $request, EntityManagerInterface $em): Response
    {
        $ecole = $this->schoolContext->getEcole();
        $classe = (new Classe())->setEcole($ecole);
        $form = $this->createForm(ClasseType::class, $classe, ['ecole' => $ecole]);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $em->persist($classe);
            $em->flush();
            $this->addFlash('success', 'La classe a été créée.');
            return $this->redirectToRoute('school_classe_show', ['id' => $classe->getId()]);
        }
        return $this->render('school/classe/form.html.twig', ['ecole' => $ecole, 'form' => $form, 'title' => 'Nouvelle classe']);
    }

    #[Route('/{id}', name: 'show', requirements: ['id' => '\\d+'], methods: ['GET'])]
    public function show(Classe $classe, EtudiantRepository $etudiantRepository): Response
    {
        $ecole = $this->schoolContext->getEcole();
        $this->schoolContext->assertSameSchool($classe->getEcole());

        return $this->render('school/classe/show.html.twig', [
            'ecole' => $ecole,
            'classe' => $classe,
            'etudiants' => $etudiantRepository->findByClasseForEcole($ecole, (int) $classe->getId()),
        ]);
    }

    #[Route('/{id}/edit', name: 'edit', requirements: ['id' => '\\d+'], methods: ['GET', 'POST'])]
    public function edit(Request $request, Classe $classe, EntityManagerInterface $em): Response
    {
        $ecole = $this->schoolContext->getEcole();
        $this->schoolContext->assertSameSchool($classe->getEcole());
        $form = $this->createForm(ClasseType::class, $classe, ['ecole' => $ecole]);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $em->flush();
            $this->addFlash('success', 'La classe a été mise à jour.');
            return $this->redirectToRoute('school_classe_show', ['id' => $classe->getId()]);
        }
        return $this->render('school/classe/form.html.twig', ['ecole' => $ecole, 'form' => $form, 'title' => 'Modifier la classe']);
    }

    #[Route('/{id}/delete', name: 'delete', requirements: ['id' => '\\d+'], methods: ['POST'])]
    public function delete(Request $request, Classe $classe, EntityManagerInterface $em): Response
    {
        $this->schoolContext->assertSameSchool($classe->getEcole());
        if ($this->isCsrfTokenValid('delete_classe_'.$classe->getId(), (string) $request->request->get('_token'))) {
            $em->remove($classe);
            $em->flush();
            $this->addFlash('success', 'La classe a été supprimée.');
        }
        return $this->redirectToRoute('school_classe_index');
    }

}
