<?php

namespace App\Controller;

use App\Entity\Category;
use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\SerializerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

/**
 * @Route("/api")
 */
class CategoryController extends Controller
{
    private $serializer;

    public function __construct(SerializerInterface $serializer)
    {
        $this->serializer = $serializer;
    }

    /**
     * Get category details
     *
     * @Route("/category/{id}", name="categoryDetail")
     * @Method("GET")
     * @Serializer\Groups("detail")
     */
    public function show(Category $category)
    {
        return $this->getJsonReponse($category);
    }

    private function getJsonReponse($data = null, int $status = 200, array $headers = []): JsonResponse
    {
        $serializedData = $data;
        if (!is_null($data)) {
            $serializedData = $this->serializer->serialize($data, 'json');
        }
        $response = new JsonResponse(null, $status, $headers);
        $response->setContent($serializedData);

        return $response;

    }
}
