<?php

namespace Lc\ShopBundle\Controller\Backend;

use App\Repository\UserRepository;
use Lc\ShopBundle\Context\NewsInterface;
use Lc\ShopBundle\Context\UserInterface;
use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class NewsController extends AdminController
{

        public function sendAction()
        {
                $idNews = $this->request->get('id') ;

                $newsletter = $this->merchantUtils->getMerchantCurrent()->getNewsletter() ;

                if($newsletter) {
                        $users = $this->em->getRepository($this->em->getClassMetadata(UserInterface::class)->getName())->findAllByNewsletter($newsletter) ;
                        $countUsers = count($users) ;
                        $messages = [];
                        $news = $this->em->getRepository($this->em->getClassMetadata(NewsInterface::class)->getName())->find($idNews) ;
                        if($news) {
                                foreach ($users as $user) {
                                        $message = new \Swift_Message('[Place du Local] '.$news->getTitle());
                                        $message->addTo($user->getEmail())
                                                ->addFrom($this->getParameter('app.noreply_email'), $this->getParameter('app.site_name'))
                                                ->setBody($this->renderView('mail/news.html.twig', [
                                                        'message' => $news->getDescription(),
                                                        'image' => $news->getImage(),
                                                        'newsletter' => $newsletter,
                                                        'user' => $user
                                                ]), 'text/html');
                                        array_push($messages, $message);
                                }

                                $result = $this->mailjetTransport->bulkSend($messages);

                                if($countUsers > 0) {
                                        $this->addFlash('success', 'Actualité envoyée à '.count($users).' utilisateurs.');

                                        $news->setIsSent(true) ;
                                        $this->em->persist($news);
                                        $this->em->flush() ;
                                }
                                else {
                                        $this->addFlash('error', 'Aucun utilisateur inscrit à la newsletter.');
                                }
                        }
                        else {
                                throw new NotFoundHttpException('Actualité introuvable') ;
                        }
                }
                else {
                        throw new \ErrorException('Aucune newsletter n\'est lié à ce Merchant.') ;
                }

                return $this->redirectToRoute('easyadmin', ['entity' => 'News', 'action' => 'list']) ;
        }

}