<?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 App\Entity\Article;
use App\Entity\ServiceRdvReservation;

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

#[Route('/frontend/panier')]
class PanierController extends AbstractController
{

    #[Route('/index', name: 'frontend_panier_index')]
    public function index(Request $request): Response
    {
        $session = $request->getSession();

        $user = $this->getUser();

        $nameSession = 'Panier-'.$user->getId();

        $paniers = $session->get($nameSession);
        
        return $this->render('frontend/reservation_panier/panier_index.html.twig', ['paniers'=>$paniers]);
    }


    #[Route('/add/article/{id}', name: 'frontend_panier_add_article')]
    public function add_article(Article $article, Request $request): Response
    {
        $qte = $request->get('qty');

        $user = $this->getUser();
        
        $avance = ($article->getPrixPromo() * $qte * 30) / 100;
        
        $panier = ['type'=>'ARTICLE', 
                    'id'=>$article->getId(), 
                    'titre'=>$article->getTitre(),
                    'image'=> "uploads/images/articles/".$article->getImageDefault(),
                    'qtyRdv'=> $qte,
                    'idRdv'=>'',
                    'prix'=>$article->getPrixPromo(),
                    'avance'=> $avance,
                    'frais'=> ($user->getFrais() > 0)? ($article->getPrixPromo() * $qte * $user->getFrais()) / 100 : 0 ,
                    'data'=>[]];

        $user->setPanier($request, $panier);

        $this->addFlash('success', ['title'=>'Success', 
                                    'message'=>"L'article a été ajouté à votre panier."]);

        $referer = $request->headers->get('referer');
       
        return $this->redirect($referer);
    }


    #[Route('/add/service/{id}', name: 'frontend_panier_add_service')]
    public function add_service(Request $request, ServiceRdvReservation $serviceRdvReservation): Response
    {
        $user = $this->getUser();

        $service = $serviceRdvReservation->getService();

        $data = ['nom'=>$request->get('nom'), 'prenom'=>$request->get('prenom'), 'naissance'=>$request->get('naissance'), 'email'=>$request->get('email')];
        
        $panier = ['type'=>'SERVICE', 
                    'id'=>$service->getId(), 
                    'titre'=>$service->getTitre(),
                    'image'=> "uploads/images/services/".$service->getImageDefault(),
                    'qtyRdv'=> $serviceRdvReservation->getDateDebut()->format('d/m/Y H:i'),
                    'idRdv'=>$serviceRdvReservation->getId(),
                    'prix'=>$service->getPrixPromo(),
                    'avance'=> ($service->getPrixPromo() * 30) / 100,
                    'frais'=>($user->getFrais() > 0)? ($service->getPrixPromo() * $user->getFrais()) / 100 : 0,
                    'data'=>$data];

        $user->setPanier($request, $panier);

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


    #[Route('/remove/{idDele}', name: 'frontend_panier_remove')]
    public function remove(Request $request, int $idDele): Response
    {
        $user = $this->getUser();

        $id = $user->getId();

        $session = $request->getSession();
        $sessionName = "Panier-$id";

        $panier = $session->get($sessionName);
        
        unset($panier[$idDele]);

        $session->set($sessionName, $panier);
		
        return $this->redirectToRoute('frontend_panier_index');
    }
}
