<?php

namespace App\Controller\Frontend;

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

use Doctrine\ORM\EntityManagerInterface;

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

use App\Repository\ServiceRdvReservationRepository;
use App\Repository\ArticleRepository;

use App\Service\Frontend\ServicePanier;

#[Route('/frontend/paiement')]
class PaiementController extends AbstractController
{

    /**
     * @var ServiceRdvReservationRepository $serviceRdvReservationRepository
     */
    private $serviceRdvReservationRepository;

    /**
     * @var ArticleRepository $articleRepository
     */
    private $articleRepository;

    /**
     * @var EntityManagerInterface $em
     */
    private $em;

    /**
     * @param ServiceRdvReservationRepository $serviceRdvReservationRepository
     * @param ArticleRepository $articleRepository
     * @param EntityManagerInterface $em
     * @return $this
     */
    public function __construct(
        ServiceRdvReservationRepository $serviceRdvReservationRepository,
        ArticleRepository $articleRepository,
        EntityManagerInterface $em
    ){
        $this->serviceRdvReservationRepository = $serviceRdvReservationRepository;
        $this->articleRepository = $articleRepository;
        $this->em = $em;
    }


    #[Route('/index', name: 'frontend_paiement_index')]
    public function index(): Response
    {
        return $this->render('frontend/reservation_panier/paiement.html.twig', [
        ]);
    }


    #[Route('/valider', name: 'frontend_paiement_valider')]
    public function valider(Request $request, ServicePanier $servicePanier): Response
    {
        $user = $this->getUser();

        $paniers = $user->getPanier($request);
        
        foreach($paniers as $panier){
            if($panier['type'] == 'SERVICE'){
                $serviceRdvReservation = $this->serviceRdvReservationRepository->find($panier['idRdv']);
                $service = $serviceRdvReservation->getService();
                $reservationService = new ReservationService();
                $reservationService->setClient($user);
                $reservationService->setServiceRdvReservation($serviceRdvReservation);
                $reservationService->setService($service);
                $reservationService->setPrix($panier['prix']);
                $reservationService->setAvance($panier['avance']);
                $reservationService->setFrais($panier['frais']);
                $reservationService->setData($panier['data']);
                $reservationService->setDateRdv($serviceRdvReservation->getDateDebut());
                $reservationService->setEtat(['payer'=>new \DateTime()]);

                $serviceRdvReservation->setDisponible(false);
                $this->em->persist($serviceRdvReservation);
                $this->em->persist($reservationService);
            }elseif($panier['type'] == 'ARTICLE'){
                $reservationArticle = new ReservationArticle();
                $article = $this->articleRepository->find($panier['id']);
                $reservationArticle->setClient($user);
                $reservationArticle->setArticle($article);

                $reservationArticle->setPrix($panier['prix']);

                $reservationArticle->setAvance($panier['avance']);
                $reservationArticle->setQuantite($panier['qtyRdv']);
                
                $reservationArticle->setFrais($panier['frais']);
                $reservationArticle->setEtat(['payer'=>new \DateTime()]);

                if($article->getLimitType() == 1){
                    $Qty = $article->getLimitQuantite();
                    $Qty -= $panier['qtyRdv'];
                    $article->setLimitQuantite($Qty);
                    $this->em->persist($article);
                }
                
                $this->em->persist($reservationArticle);
            }
        }
        $this->em->flush();

        $servicePanier->clear($user);

        return $this->redirectToRoute('frontend_reservation_index');
    }

}
