<?php

namespace Lc\ShopBundle\Controller\Backend;

use Doctrine\ORM\EntityManagerInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use Lc\ShopBundle\Context\DocumentInterface;
use Lc\ShopBundle\Context\MerchantUtilsInterface;
use Lc\ShopBundle\Context\OrderUtilsInterface;
use Lc\ShopBundle\Services\Utils;
use Lc\ShopBundle\Services\UtilsManager;
use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;

class DocumentController extends AdminController
{
        protected $documentRepository ;

        public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em, MailjetTransport $mailjetTransport, UtilsManager $utilsManager, TranslatorInterface $translator)
        {
                parent::__construct($security, $userManager, $em, $mailjetTransport, $utilsManager, $translator);
                $this->documentRepository = $this->em->getRepository($this->em->getClassMetadata(DocumentInterface::class)->getName()) ;
        }

        public function downloadInvoiceAction()
        {
                $idDocument = $this->request->get('id') ;
                $document = $this->documentRepository->find($idDocument) ;

                $orderShop = null ;
                if($document) {
                        $orderShops = $document->getOrderShops() ;
                        $orderShop = (isset($orderShops[0])) ? $orderShops[0] : null ;
                }

                if($document && $orderShop) {
                        $this->orderUtils->generateDocumentInvoiceOrderShop($orderShop, 'download') ;
                }
                else {
                        throw new NotFoundHttpException('Document introuvable') ;
                }
        }

}