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

use App\Entity\EntCustomer;
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 CustomerViewController
 * @package App\Controller
 * @IsGranted("ROLE_USER")
 */
class CustomerViewController extends AbstractController
{
    /**
     * @Route("/get-customer-data", name="get_customer_data")
     */
    public function getCustomerData()
    {
        $em = $this->getDoctrine()->getManager();
        return Reply::getResponse(
            [
                'customers' => Utils::clientMap($em, $em->getRepository('App:EntCustomer')->findAll()),
                'customerContacts' => Utils::clientMap($em, $em->getRepository('App:EntCustomerContact')->findAll()),
                'customerMeetings' => Utils::clientMap($em, $em->getRepository('App:EntCustomerMeeting')->findAll()),
            ]
        );
    }

    /**
     * @Route("/get-customer-full", name="get_customer_full")
     * @param Request $request
     * @return JsonResponse
     */
    public function getCustomerFull(Request $request)
    {
        $customerId = json_decode($request->request->get('customerId'));
        $em = $this->getDoctrine()->getManager();
        /** @var EntCustomer $entCustomer */
        $entCustomer = $em->getRepository('App:EntCustomer')->find($customerId);
        if (is_null($entCustomer)) {
            return Reply::getErrorResponse(Message::ERROR_DEFAULT);
        }

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

}