<?php

namespace App\Controller;

use App\Entity\Employee;
use App\Entity\InternApplication;
use App\Form\EmployeeType;
use App\Repository\AnnouncementRepository;
use App\Repository\EmployeeRepository;
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('/employee')]
final class EmployeeController extends AbstractController
{
    public function __construct(
        private readonly EntityManagerInterface $entityManager,
        private readonly EmployeeRepository $employeeRepository,
        private readonly AnnouncementRepository $announcementRepository,
        private readonly InternApplicationRepository $internApplicationRepository
    )
    {
    }
    #[Route(name: 'app_employee_index', methods: ['GET'])]
    public function index(): Response
    {
        return $this->render('employee/index.html.twig', [
            'employees' => $this->employeeRepository->findAll(),
        ]);
    }

    #[Route('/{id}', name: 'app_employee_show', requirements: ['id' => '\d+'], methods: ['GET'])]
    public function show(Employee $employee): Response
    {
        return $this->render('employee/show.html.twig', [
            'employee' => $employee,
        ]);
    }

    #[Route('/{id}/edit', name: 'app_employee_edit', requirements: ['id' => '\d+'], methods: ['GET', 'POST'])]
    public function edit(Request $request, Employee $employee): Response
    {
        $form = $this->createForm(EmployeeType::class, $employee);
        $form->handleRequest($request);

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

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

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

    #[Route('/{id}', name: 'app_employee_delete', requirements: ['id' => '\d+'], methods: ['POST'])]
    public function delete(Request $request, Employee $employee): Response
    {
        if ($this->isCsrfTokenValid('delete'.$employee->getId(), $request->getPayload()->getString('_token'))) {
            $this->entityManager->remove($employee);
            $this->entityManager->flush();
        }

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

    #[Route('/seeApplications', name: 'app_employee_seeApplications')]
    public function seeApplications(): Response
    {
        $employee = $this->getUser();

        if (!$employee instanceof Employee){
            throw $this->createAccessDeniedException('Seuls les employés peuvent accéder à cette page.');
        }

        $company = $employee->getCompany();

        $announcements = $this->announcementRepository
            ->findBy(['company' => $company]);

        $applications = $this->internApplicationRepository
            ->findBy(['application' => $announcements]);

        return $this->render('employee/applications.html.twig', [
            'applications' => $applications,
        ]);
    }

    #[Route('/application/{id}/update-status/{status}', name: 'app_employee_update_application_status', methods: ['POST'])]
    public function updateApplicationStatus(int $id, string $status): Response
    {
        $employee = $this->getUser();

        if (!$employee instanceof Employee) {
            throw $this->createAccessDeniedException('Seuls les employés peuvent accéder à cette action.');
        }

        $application = $this->internApplicationRepository->find($id);

        if (!$application) {
            throw $this->createNotFoundException('Candidature introuvable.');
        }

        $company = $employee->getCompany();
        if ($application->getApplication()->getCompany() !== $company) {
            throw $this->createAccessDeniedException('Cette candidature ne vous appartient pas.');
        }

        $application->setStatus($status);
        $this->entityManager->flush();

        $this->addFlash('success', 'Le statut de la candidature a bien été mis à jour.');

        return $this->redirectToRoute('app_employee_seeApplications');
    }
}
