<?php

namespace App\Controller;

use App\Entity\VocabFuncContext;
use App\Security\Voter\VocabVoter;
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\Core\Exception\AccessDeniedException;

/**
 * @todo Make edit endpoints JSON?
 */
class VocabFuncContextController extends AbstractController
{
    #[Route('/vocabs/functional_context', name: 'app_vocab_func_context')]
    public function index(EntityManagerInterface $em): Response
    {
        $roles = $this->getUser()->getRoles();

        if (in_array('ROLE_READER', $roles)) {
            $this->addFlash('warning', 'Only editors, revisors and administrators can view vocabularies');
            return $this->redirectToRoute('app_home');
        }

        $terms = $em->getRepository(VocabFuncContext::class)->findBy([], ['term' => 'ASC']);

        return $this->render('vocab_func_context/index.html.twig', [
            'controller_name' => 'VocabFuncContextController',
            'terms' => $terms,
        ]);
    }

    #[Route('/vocabs/functional_context/add', name: 'app_vocab_func_context_add')]
    public function addTerm(Request $request, EntityManagerInterface $em): Response
    {
        $vocab = new VocabFuncContext;
        try {
            $this->denyAccessUnlessGranted(VocabVoter::EDIT, $vocab);
        }
        catch (AccessDeniedException) {
            $this->addFlash('warning', 'Only revisors and administrators can edit vocabularies');
            return $this->redirectToRoute('app_home');
        }

        $term = $request->getPayload()->get('_term');
        $vocab->setTerm($term);
        $em->persist($vocab);
        $em->flush();

        $this->addFlash('notice', 'Term added successfully');

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

    #[Route('/vocabs/functional_context/update', name: 'app_vocab_func_context_upd')]
    public function updateTerm(Request $request, EntityManagerInterface $em): Response
    {
        $vocab = new VocabFuncContext;
        try {
            $this->denyAccessUnlessGranted(VocabVoter::EDIT, $vocab);
        }
        catch (AccessDeniedException) {
            $this->addFlash('warning', 'Only revisors and administrators can edit vocabularies');
            return $this->redirectToRoute('app_home');
        }
        $id = $request->getPayload()->get('_id');
        $newTerm = $request->getPayload()->get('_new_term');

        $repo = $em->getRepository(VocabFuncContext::class);
        $vocab = $repo->find($id);
        $vocab->setTerm($newTerm);
        $em->flush();

        $this->addFlash('notice', 'Term updated successfully');

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


    #[Route('/vocabs/functional_context/del', name: 'app_vocab_func_context_del')]
    public function deleteTerm(Request $request, EntityManagerInterface $em): Response
    {
        $vocab = new VocabFuncContext;
        try {
            $this->denyAccessUnlessGranted(VocabVoter::EDIT, $vocab);
        }
        catch (AccessDeniedException) {
            $this->addFlash('warning', 'Only revisors and administrators can delete vocabularies');
            return $this->redirectToRoute('app_home');
        }

        $id = $request->getPayload()->get('_id');
        $repo = $em->getRepository(VocabFuncContext::class);
        $term = $repo->find($id);
        $em->remove($term);
        $em->flush();

        $this->addFlash('notice', 'Term deleted successfully');

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