<?php

namespace App\Controller\BackendPartner;

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

use Symfony\Component\HttpFoundation\Request;

use App\Entity\Societe;

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

use App\Repository\ReservationArticleValidationRepository;
use App\Repository\ReservationServiceValidationRepository;

#[Route('/backend/partner/reservation/liste')]
class ReservationListeController extends AbstractController
{
    /**
     * @var ReservationArticleRepository $reservationArticleRepository
     */
    private $reservationArticleRepository;

    /**
     * @var ReservationArticleValidationRepository $reservationArticleValidationRepository
     */
    private $reservationArticleValidationRepository;

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

    /**
     * @var ReservationServiceValidationRepository $reservationServiceValidationRepository
     */
    private $reservationServiceValidationRepository;

    /**
     * @param ReservationArticleRepository $reservationArticleRepository
     * @param ReservationArticleValidationRepository $reservationArticleValidationRepository
     * @param ReservationServiceRepository $reservationServiceRepository
     * @param ReservationServiceValidationRepository $reservationServiceValidationRepository
     * @return $this
     */
    public function __construct(
        ReservationArticleRepository $reservationArticleRepository,
        ReservationArticleValidationRepository $reservationArticleValidationRepository,
        ReservationServiceRepository $reservationServiceRepository,
        ReservationServiceValidationRepository $reservationServiceValidationRepository
    ){
        
        $this->reservationArticleRepository = $reservationArticleRepository;
        $this->reservationArticleValidationRepository = $reservationArticleValidationRepository;
        $this->reservationServiceRepository = $reservationServiceRepository;
        $this->reservationServiceValidationRepository = $reservationServiceValidationRepository;
    }
    

    #[Route('/valider', name: 'backend_partner_reservation_liste_valider')]
    public function valider(Request $request): Response
    {
        $societe = $this->getUser()->getSelectPartner()->getSociete();

        $reservationArticleValidations = $this->reservationArticleValidationRepository->findBySociete($societe);
        
        $reservationServiceValidations = $this->reservationServiceValidationRepository->findBySociete($societe);
        
        $reservationValidations = array_merge($reservationArticleValidations, $reservationServiceValidations);

        usort($reservationValidations, function($a, $b) {
            return $a->getDateAdd()->getTimestamp() - $b->getDateAdd()->getTimestamp();
        });

        return $this->render('backend_partner/reservation/liste_valider.html.twig',[
            'reservationValidations'=>$reservationValidations
        ]);
    }

    
    
    #[Route('/encours', name: 'backend_partner_reservation_liste_encours')]
    public function encours(Request $request): Response
    {
        $societe = $this->getUser()->getSelectPartner()->getSociete();

        $reservationArticles = $this->reservationArticleRepository->findBySociete($societe);
        
        $reservationServices = $this->reservationServiceRepository->findBySociete($societe);

        $reservations = array_merge($reservationArticles, $reservationServices);
        
        usort($reservations, function($a, $b) {
            return $a->getDateAdd()->getTimestamp() - $b->getDateAdd()->getTimestamp();
        });

        return $this->render('backend_partner/reservation/liste_encours.html.twig', [
            'reservations'=>$reservations

        ]);
    }

}