<?php

declare(strict_types=1);

namespace App\Controller\School;

use App\Service\School\SchoolContext;
use App\Entity\Ecole;
use App\Entity\Niveau;
use App\Form\NiveauType;
use App\Repository\NiveauRepository;
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/niveaux', name: 'school_niveau_')]
#[IsGranted('ROLE_ECOLE_ADMIN')]
final class NiveauController extends AbstractController
{
    public function __construct(private readonly SchoolContext $schoolContext)
    {
    }

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

    #[Route('/new', name: 'new', methods: ['GET', 'POST'])]
    public function new(Request $request, EntityManagerInterface $em): Response
    {
        $ecole = $this->schoolContext->getEcole();
        $niveau = (new Niveau())->setEcole($ecole)->setOrdre(0);
        $form = $this->createForm(NiveauType::class, $niveau, ['ecole' => $ecole]);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) { $em->persist($niveau); $em->flush(); $this->addFlash('success', 'Le niveau a été créé.'); return $this->redirectToRoute('school_niveau_index'); }
        return $this->render('school/niveau/form.html.twig', ['ecole' => $ecole, 'form' => $form, 'title' => 'Nouveau niveau']);
    }

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

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

}
