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

use App\Entity\EntProduction;
use App\Entity\EntProductionContact;
use App\Entity\EntProductionMeeting;
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 ProductionViewController
 * @package App\Controller
 * @IsGranted("ROLE_USER")
 */
class ProductionViewController extends AbstractController
{
    /**
     * @Route("/get-production-data", name="get_production_data")
     */
    public function getProductionData()
    {
        $em = $this->getDoctrine()->getManager();
        return Reply::getResponse(
            [
                'productions' => Utils::clientMap($em, $em->getRepository(EntProduction::class)->findAll()),
                'productionContacts' => Utils::clientMap($em, $em->getRepository(EntProductionContact::class)->findAll()),
                'productionMeetings' => Utils::clientMap($em, $em->getRepository(EntProductionMeeting::class)->findAll()),
            ]
        );
    }

    /**
     * @Route("/get-production-full", name="get_production_full")
     * @param Request $request
     * @return JsonResponse
     */
    public function getProductionFull(Request $request)
    {
        $productionId = json_decode($request->request->get('productionId'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntProduction $entProduction */
        $entProduction = $em->getRepository(EntProduction::class)->find($productionId);
        if (is_null($entProduction)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

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

}