<?php

namespace App\Controller\Frontend;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

use App\Repository\ReservationArticleRepository;
use App\Repository\ReservationServiceRepository;

use App\Entity\ReservationArticle;
use App\Entity\ReservationService;

use App\Service\Frontend\ServiceReservation;

#[Route('/frontend/reservation')]
class ReservationController extends AbstractController
{

    /**
     * @var ReservationServiceRepository $reservationServiceRepository
     */
    private $reservationServiceRepository;

    /**
     * @var ReservationArticleRepository $reservationArticleRepository
     */
    private $reservationArticleRepository;

    /**
     * @var ServiceReservation $serviceReservation
     */
    private $serviceReservation;

    /**
     * @param ReservationServiceRepository $reservationServiceRepository
     * @param ReservationArticleRepository $reservationArticleRepository
     * @param ServiceReservation $serviceReservation
     * @return $this
     */
    public function __construct(
        ReservationServiceRepository $reservationServiceRepository,
        ReservationArticleRepository $reservationArticleRepository,
        ServiceReservation $serviceReservation
    ){
        $this->reservationServiceRepository = $reservationServiceRepository;
        $this->reservationArticleRepository = $reservationArticleRepository;
        $this->serviceReservation = $serviceReservation;
    }

    #[Route('/index', name: 'frontend_reservation_index')]
    public function index(): Response
    {
        $user = $this->getUser();
        
        $reservationServices = $this->reservationServiceRepository->findBy(['client'=>$user]);
        $reservationArticles = $this->reservationArticleRepository->findBy(['client'=>$user]);

        $reservations = array_merge($reservationServices, $reservationArticles);

        usort($reservations,function($first,$second){
            return $first->getDateAdd() < $second->getDateAdd();
        });

        return $this->render('frontend/reservation_panier/reservation_index.html.twig', [
            'reservations'=>$reservations
        ]);

    }

    #[Route('/export/pdf/article/{id}', name: 'frontend_reservation_export_pdf_article')]
    public function export_pdf_article(ReservationArticle $reservationArticle): Response
    {
        $html = $this->renderView('frontend/reservation_panier/reservation_template_pdf_article.html.twig', ['reservationArticle'=>$reservationArticle]);

        $idReservation = $reservationArticle->getId();
        $this->serviceReservation->generatePdfFile($html, "réservation_article_$idReservation.pdf");

        return new Response();
    }

    #[Route('/export/pdf/service/{id}', name: 'frontend_reservation_export_pdf_service')]
    public function export_pdf_service(ReservationService $reservationService): Response
    {
        $idReservation = $reservationService->getId();

        $html = $this->renderView('frontend/reservation_panier/reservation_template_pdf_service.html.twig', ['reservationService'=>$reservationService]);

        $this->serviceReservation->generatePdfFile($html, "réservation_service_$idReservation.pdf");

        return new Response($html);
    }

}
