<?php

namespace App\Controller;

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

class CommentController extends AbstractController
{
    public function __construct(
        protected EntityManagerInterface $entityManager
    )
    {

    }

    #[Route('/comments', name: 'app_comments')]
    public function index(Serializer $serializer): Response
    {
        $comments = $this->entityManager->getRepository(Comment::class)->findAll();
        $response = ['result' => 'ok'];
        foreach($comments as $comment)
        {
            $response['comments'][] = $serializer->normalize($comment);
        }

        return new JsonResponse($response);
    }
}
