<?php

namespace App\Controller;

use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use App\Entity\VocabObjectType;

/**
 * @todo Pagination
 */
class VocabObjectTypeController extends AbstractController
{
    #[Route('/vocabs/object_type', name: 'app_vocab_object_type')]
    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(VocabObjectType::class)
            ->findBy([], ['term' => 'ASC']);

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