<?php

namespace App\Controller\Admin;

use App\Entity\Tenant\Comment;
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;

class CommentController extends AbstractController
{
    public function __construct(
        private readonly EntityManagerInterface $entityManager,
    ) {}

    #[Route('/comments', name: 'admin_comments')]
    public function index(Request $request): Response
    {
        $qb = $this->entityManager->getRepository(Comment::class)
            ->createQueryBuilder('c')
            ->leftJoin('c.news', 'n')
            ->addSelect('n')
            ->orderBy('c.createdAt', 'DESC');

        $status = $request->query->get('status');
        if ($status) {
            $qb->andWhere('c.status = :status')->setParameter('status', $status);
        }

        $comments = $qb->getQuery()->getResult();

        return $this->render('admin/comment/index.html.twig', [
            'comments' => $comments,
            'currentStatus' => $status,
        ]);
    }

    #[Route('/comments/{id}/approve', name: 'admin_comment_approve', methods: ['POST'])]
    public function approve(int $id, Request $request): Response
    {
        $comment = $this->entityManager->getRepository(Comment::class)->find($id);
        if (!$comment) {
            throw $this->createNotFoundException('Yorum bulunamadı.');
        }

        if (!$this->isCsrfTokenValid('comment_action_' . $id, $request->request->get('_token'))) {
            $this->addFlash('error', 'Geçersiz CSRF token. Lütfen tekrar deneyin.');

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

        $comment->setStatus('approved');
        $this->entityManager->flush();

        $this->addFlash('success', 'Yorum başarıyla onaylandı.');

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

    #[Route('/comments/{id}/reject', name: 'admin_comment_reject', methods: ['POST'])]
    public function reject(int $id, Request $request): Response
    {
        $comment = $this->entityManager->getRepository(Comment::class)->find($id);
        if (!$comment) {
            throw $this->createNotFoundException('Yorum bulunamadı.');
        }

        if (!$this->isCsrfTokenValid('comment_action_' . $id, $request->request->get('_token'))) {
            $this->addFlash('error', 'Geçersiz CSRF token. Lütfen tekrar deneyin.');

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

        $comment->setStatus('rejected');
        $this->entityManager->flush();

        $this->addFlash('success', 'Yorum reddedildi.');

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

    #[Route('/comments/{id}/delete', name: 'admin_comment_delete', methods: ['POST'])]
    public function delete(int $id, Request $request): Response
    {
        $comment = $this->entityManager->getRepository(Comment::class)->find($id);
        if (!$comment) {
            throw $this->createNotFoundException('Yorum bulunamadı.');
        }

        if ($this->isCsrfTokenValid('delete_comment_' . $id, $request->request->get('_token'))) {
            $this->entityManager->remove($comment);
            $this->entityManager->flush();
            $this->addFlash('success', 'Yorum başarıyla silindi.');
        } else {
            $this->addFlash('error', 'Geçersiz CSRF token. Silme işlemi gerçekleştirilemedi.');
        }

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