<?php

declare(strict_types=1);

namespace App\Controller\School;

use App\Service\School\SchoolContext;
use App\Entity\Ecole;
use App\Entity\Section;
use App\Form\SectionType;
use App\Repository\SectionRepository;
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/sections', name: 'school_section_')]
#[IsGranted('ROLE_ECOLE_ADMIN')]
final class SectionController extends AbstractController
{
    public function __construct(private readonly SchoolContext $schoolContext)
    {
    }

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

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

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

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

}
