<?php

namespace App\Controller\Brain;

use App\Entity\Tag;
use App\Form\TagType;

use Doctrine\ORM\EntityManagerInterface;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;

final class BrainTagController extends AbstractController
{
    #[Route('/brain/tag/list', name: 'brain_tags_list')]
    public function index(EntityManagerInterface $entityManager): Response
    {

        $tags = $entityManager->getRepository(Tag::class)->findAll();

        return $this->render('brain/taxonomy/index.html.twig', [
            'taxonomy' => $tags,
            'type' => 'Tag'
        ]);
    }

    #[Route('/brain/tag/new', name: 'brain_tags_new')]
    public function new(EntityManagerInterface $entityManager, Request $request): Response
    {

        $form = $this->createForm(TagType::class);

        $form->handleRequest($request);

        if ($form->isSubmitted()) {
            $data = $form->getData();
            $Tag = new Tag();

            $Tag->setTitle($data->getTitle());

            $entityManager->persist($Tag);

            $entityManager->flush();

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

        return $this->render('brain/taxonomy/create.html.twig', [
            'action' => 'New',
            'form' => $form,
            'type' => 'Tag'
        ]);
    }
}
