<?php

namespace App\Controller;

use App\Entity\Degree;
use App\Entity\InternDegree;
use App\Form\DegreeType;
use App\Repository\AnnouncementRepository;
use App\Repository\DegreeRepository;
use App\Repository\InternApplicationRepository;
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;

#[Route('/degree')]
final class DegreeController extends AbstractController
{
    public function __construct(
        private readonly EntityManagerInterface $entityManager,
        private readonly DegreeRepository $degreeRepository,
    ){}
    #[Route(name: 'app_degree_index', methods: ['GET'])]
    public function index(): Response
    {
        return $this->render('degree/index.html.twig', [
            'degrees' => $this->degreeRepository->findAll(),
        ]);
    }

    #[Route('/new', name: 'app_degree_new', methods: ['GET', 'POST'])]
    public function new(Request $request,): Response
    {
        $degree = new Degree();
        $form = $this->createForm(DegreeType::class, $degree);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->entityManager->persist($degree);
            $this->entityManager->flush();

            return $this->redirectToRoute('app_degree_index', [], Response::HTTP_SEE_OTHER);
        }

        return $this->render('degree/new.html.twig', [
            'degree' => $degree,
            'form' => $form,
        ]);
    }

    #[Route('/{id}', name: 'app_degree_show', methods: ['GET'])]
    public function show(Degree $degree): Response
    {
        return $this->render('degree/show.html.twig', [
            'degree' => $degree,
        ]);
    }

    #[Route('/{id}/edit', name: 'app_degree_edit', methods: ['GET', 'POST'])]
    public function edit(Request $request, Degree $degree,): Response
    {
        $form = $this->createForm(DegreeType::class, $degree);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->entityManager->flush();

            return $this->redirectToRoute('app_degree_index', [], Response::HTTP_SEE_OTHER);
        }

        return $this->render('degree/edit.html.twig', [
            'degree' => $degree,
            'form' => $form,
        ]);
    }

    #[Route('/{id}', name: 'app_degree_delete', methods: ['POST'])]
    public function delete(Request $request, Degree $degree,): Response
    {
        if ($this->isCsrfTokenValid('delete'.$degree->getId(), $request->getPayload()->getString('_token'))) {
            $this->entityManager->remove($degree);
            $this->entityManager->flush();
        }

        return $this->redirectToRoute('app_degree_index', [], Response::HTTP_SEE_OTHER);
    }
}
