<?php

namespace App\Controller\Frontend;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\ExpressionLanguage\Expression;

use App\Form\Frontend\CommentType;

use App\Entity\Article;
use App\Entity\Service;
use App\Entity\Annonce;
use App\Entity\CommentArticle;
use App\Entity\CommentService;
use App\Entity\CommentAnnonce;



use Doctrine\ORM\EntityManagerInterface;

#[Route('/frontend/comment')]
#[IsGranted(new Expression('is_granted("ROLE_USER")'))]
class CommentController extends AbstractController
{

    /**
     * @var EntityManagerInterface $em
     */
    private $em;

    /**
     * @param EntityManagerInterface $em
     * @return $this
     */
    public function __construct(
        EntityManagerInterface $em
    ){
        $this->em = $em;
    }

    #[Route('/article/add/{id}', name: 'frontend_comment_article_add')]
    public function article_add(Article $article, Request $request): Response
    {
        $id = $article->getId();

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

        $form->handleRequest($request);
		if ($form->isSubmitted() && $form->isValid()) {

            $commentaire = $form->getData()['commentaire'];
            
            $commentArticle = new CommentArticle();
            $commentArticle->setCommentaire($commentaire);
            $commentArticle->setArticle($article);
            $commentArticle->setClient($this->getUser());

			$this->em->persist($commentArticle);
			$this->em->flush();

            return $this->redirectToRoute('frontend_article_detail', ['id'=>$id]);
		}
       
        return $this->render('frontend/comment/add.html.twig', [
            'form' => $form,
            'id' => $id,
            'pathComment'=> "/frontend/comment/article/add/$id"
        ]);
    }


    #[Route('/service/add/{id}', name: 'frontend_comment_service_add')]
    public function service_add(Service $service, Request $request): Response
    {
        $id = $service->getId();
        $form = $this->createForm(CommentType::class);

        $form->handleRequest($request);
		if ($form->isSubmitted() && $form->isValid()) {

            $commentaire = $form->getData()['commentaire'];
            
            $commentService = new CommentService();
            $commentService->setCommentaire($commentaire);
            $commentService->setService($service);
            $commentService->setClient($this->getUser());

			$this->em->persist($commentService);
			$this->em->flush();
            
            return $this->redirectToRoute('frontend_service_detail', ['id'=>$id]);
		}
        
        return $this->render('frontend/comment/add.html.twig', [
            'form' => $form,
            'id' => $id,
            'pathComment'=> "/frontend/comment/service/add/$id"
        ]);
    }

    #[Route('/annonce/add/{id}', name: 'frontend_comment_annonce_add')]
    public function annonce_add(Annonce $annonce, Request $request): Response
    {
        $id = $annonce->getId();

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

        $form->handleRequest($request);
		if ($form->isSubmitted() && $form->isValid()) {

            $commentaire = $form->getData()['commentaire'];
            
            $commentAnnonce = new CommentAnnonce();
            $commentAnnonce->setCommentaire($commentaire);
            $commentAnnonce->setAnnonce($annonce);
            $commentAnnonce->setClient($this->getUser());

			$this->em->persist($commentAnnonce);
			$this->em->flush();

            return $this->redirectToRoute('frontend_annonce_detail', ['id'=>$id]);
		}
       
        return $this->render('frontend/comment/add.html.twig', [
            'form' => $form,
            'id' => $id,
            'pathComment'=> "/frontend/comment/annonce/add/$id"
        ]);
    }

}
