<?php

namespace App\Controller;

use App\Entity\Announcement;
use App\Form\AnnouncementType;
use App\Repository\AnnouncementRepository;
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('/announcement')]
final class AnnouncementController extends AbstractController
{
    #[Route('/', name: 'app_announcement_index')]
    public function list(Request $request, AnnouncementRepository $announcementRepository): Response
    {
        $user = $this->getUser();
        $announcements = [];

        if (in_array('ROLE_ADMIN', $user->getRoles())) {
            $showNonValidated = $request->query->get('show_non_validated');

            if ($showNonValidated) {
                $announcements = $announcementRepository->findBy(['status' => 'notVerified']);
            }
            if (!$showNonValidated){
                $announcements = $announcementRepository->findAll();
            }

        }

        if (in_array('ROLE_EMPLOYEE', $user->getRoles())) {
            $company = $user->getCompany();

            if ($company)
            {
                $announcements = $announcementRepository->findBy(['company' => $company]);
            }
        }

        if (in_array('ROLE_INTERN', $user->getRoles()))
        {
            $announcements = $announcementRepository->findBy(['status' => 'Verified']);
        }

        return $this->render('announcement/index.html.twig', [
            'announcements' => $announcements,
            'showNonValidated' => $request->query->get('show_non_validated', false),
        ]);
    }


    #[Route('/new', name: 'app_announcement_new', methods: ['GET', 'POST'])]
    public function new(Request $request, EntityManagerInterface $entityManager): Response
    {
        $announcement = new Announcement();
        $form = $this->createForm(AnnouncementType::class, $announcement);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $announcement->setCreationDate(new \DateTime());
            $announcement->setStatus('notVerified');
            $entityManager->persist($announcement);
            $entityManager->flush();

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

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

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

    #[Route('/{id}/edit', name: 'app_announcement_edit', methods: ['GET', 'POST'])]
    public function edit(Request $request, Announcement $announcement, EntityManagerInterface $entityManager): Response
    {
        $form = $this->createForm(AnnouncementType::class, $announcement);
        $form->handleRequest($request);

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

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

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

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

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

    #[Route('/{id}/validate', name: 'app_announcement_validate', methods: ['POST'])]
    public function validate(Request $request, Announcement $announcement, EntityManagerInterface $entityManager): Response
    {
        if ($announcement->getStatus() !== 'Verified')
        {
            $announcement->setStatus('Verified');
            $entityManager->flush();
        }
        return $this->redirectToRoute('app_announcement_show',
            ['id' => $announcement->getId()], Response::HTTP_SEE_OTHER);
    }

}
