<?php

namespace App\Controller\Brain;

use App\Entity\Category;
use App\Entity\Tag;
use App\Form\PostType;
use App\Entity\Post;

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 BrainPostController extends AbstractController
{
    #[Route('/brain/post/list', name: 'brain_post_list')]
    public function index(EntityManagerInterface $entityManager): Response 
    {
        $posts = $entityManager->getRepository(Post::class)->findAllOrderByLatest();
        
        return $this->render('brain/post/index.html.twig', [
            'posts' => $posts
        ]);
    }

    #[Route('/brain/post/new', name: 'brain_post_new')]
    public function new(EntityManagerInterface $entityManager, Request $request): Response
    {
        $form = $this->createForm(PostType::class);

        $form->handleRequest($request);

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

            $post->setTitle($data->getTitle());
            $post->setDate($data->getDate());
            $post->setText($data->getText());
            $post->setUrl($data->getUrl());
            
            foreach ($data->getTags() as $tag) {
                $post->addTag($tag);
            }

            if ($data->isPublished()) {
                $post->setPublished(true);
            } else {
                $post->SetPublished(false);
            }
            
            $tax = $request->request->all('post');

            if ($data->getCategory() == null) {
                $cat = $tax['category'];

                if ($cat !== null) {
                    $categoryToAdd = new Category()->setTitle($cat);
                    $post->setCategory($categoryToAdd);
                
                    $entityManager->persist($categoryToAdd);
                }
            } else {
                $post->setCategory($data->getCategory());
            }

            $entityManager->persist($post);

            $entityManager->flush();

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

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

    #[Route('/brain/post/edit/{id}', name: 'brain_post_edit')]
    public function edit(EntityManagerInterface $entityManager, string $id, Request $request): Response
    {  
        $post = $entityManager->getRepository(Post::class)->findOneBy(['id' => $id]);
        
        $form = $this->createForm(PostType::class, $post);

        $form->handleRequest($request);

        if ($form->isSubmitted()) {
            $data = $form->getData();
            //$post = new Post();


            $post->setTitle($data->getTitle());
            $post->setDate($data->getDate());
            $post->setText($data->getText());
            $post->setUrl($data->getUrl());
            
            foreach ($data->getTags() as $tag) {
                $post->addTag($tag);
            }

            if ($data->isPublished()) {
                $post->setPublished(true);
            } else {
                $post->SetPublished(false);
            }
            
            $tax = $request->request->all('post');

            if ($data->getCategory() == null) {
                $cat = $tax['category'];

                if ($cat !== null) {
                    $categoryToAdd = new Category()->setTitle($cat);
                    $post->setCategory($categoryToAdd);
                
                    $entityManager->persist($categoryToAdd);
                }
            } else {
                $post->setCategory($data->getCategory());
            }

            $entityManager->flush();

            return $this->redirectToRoute('brain_post_list');
        }
        return $this->render('brain/post/create.html.twig', [
            'action' => 'edit',
            'form' => $form
        ]);
    }
}
