<?php
/**
 * Created by PhpStorm.
 * User: danielknudsen
 * Date: 05.04.18
 * Time: 13:09
 */
namespace App\Controller;

use App\Entity\EntService;
use App\Entity\EntServiceContact;
use App\Entity\EntServiceMeeting;
use App\Utils\Message;
use App\Utils\Reply;
use App\Utils\Utils;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
 * Class ServiceViewController
 * @package App\Controller
 * @IsGranted("ROLE_USER")
 */
class ServiceViewController extends AbstractController
{
    /**
     * @Route("/get-service-data", name="get_service_data")
     */
    public function getServiceData()
    {
        $em = $this->getDoctrine()->getManager();
        return Reply::getResponse(
            [
                'services' => Utils::clientMap($em, $em->getRepository(EntService::class)->findAll()),
                'serviceContacts' => Utils::clientMap($em, $em->getRepository(EntServiceContact::class)->findAll()),
                'serviceMeetings' => Utils::clientMap($em, $em->getRepository(EntServiceMeeting::class)->findAll()),
            ]
        );
    }

    /**
     * @Route("/get-service-full", name="get_service_full")
     * @param Request $request
     * @return JsonResponse
     */
    public function getServiceFull(Request $request)
    {
        $serviceId = json_decode($request->request->get('serviceId'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntService $entService */
        $entService = $em->getRepository(EntService::class)->find($serviceId);
        if (is_null($entService)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

        return Reply::getResponse($entService->clientMapper($em, true));
    }

}