<?php

namespace App\Controller\Dashboard;

use App\Entity\AuditLog;
use App\Entity\Contrats;
use App\Logger\AppLogger;
use App\Repository\AccountRepository;
use App\Repository\AuditLogRepository;
use App\Repository\ContratsRepository;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;

class ReservationController extends AbstractController
{

    #[Route(path: '/crm/reservation', name: 'app_crm_reservation', methods: ['GET'])]
    public function crmReservation(AppLogger $appLogger): Response
    {
        $appLogger->record('VIEW', 'Affichage liste des reservations');
        return $this->render('dashboard/reservation.twig');
    }

    /**
     * Endpoint pour alimenter le calendrier en JSON
     */
    #[Route(path: '/crm/reservation/data', name: 'app_crm_reservation_data', methods: ['GET'])]
    public function getReservationData(Request $request, ContratsRepository $contratsRepository, \App\Repository\ProductBlockedRepository $productBlockedRepository): JsonResponse
    {
        $start = new \DateTimeImmutable($request->query->get('start'));
        $end = new \DateTimeImmutable($request->query->get('end'));
        
        /** @var Contrats[] $reservations */
        $reservations = $contratsRepository->findBetweenDates($start, $end);
        $events = [];
        
        foreach ($reservations as $reservation) {
            if($reservation->getProductReserves()->count() >0) {
               $events[] = [
                    'title' => 'Contrat #'.$reservation->getNumReservation(),
                    'start' => $reservation->getDateAt()->format(\DateTimeInterface::ATOM),
                    'end' => $reservation->getEndAt()->format(\DateTimeInterface::ATOM),
                    'extendedProps' => [
                        'type' => 'reservation',
                        'start' => $reservation->getDateAt()->format('d/m/Y'),
                        'end' => $reservation->getEndAt()->format('d/m/Y'),
                        'contractNumber' => $reservation->getCustomer()->getPhone(),
                        'client' => $reservation->getCustomer()->getName()." ".$reservation->getCustomer()->getSurname(),
                        'clientEmail' => $reservation->getCustomer()->getEmail(),
                        'clientPhone' => $reservation->getCustomer()->getPhone(),
                        'eventAdresse' => [
                            'address' => $reservation->GetAddressEvent(),
                            'address1' => $reservation->getAddress2Event(),
                            'address2' => $reservation->getAddress3Event(),
                            'zipCode' => $reservation->getZipCodeEvent(),
                            'city' => $reservation->getZipCodeEvent(),
                        ],
                        'linkContrat' => $this->generateUrl('app_crm_reservation_data', ['id' => $reservation->getId()]),
                        'caution' => $reservation->isCaution(),
                        'acompte' => $reservation->isAccompte(),
                        'solde' => $reservation->isSolde(),
                        'isSigned' => $reservation->isSigned(),
                    ]
               ];
            }
        }

        $blockedProducts = $productBlockedRepository->findBetweenDates($start, $end);
        foreach ($blockedProducts as $blocked) {
            $events[] = [
                'title' => 'BLOCAGE: ' . $blocked->getProduct()->getName(),
                'start' => $blocked->getDateStart()->format(\DateTimeInterface::ATOM),
                'end' => $blocked->getDateEnd()->format(\DateTimeInterface::ATOM),
                'color' => '#ef4444', // Red for blocked
                'extendedProps' => [
                    'type' => 'blocked',
                    'reason' => $blocked->getReason(),
                    'productName' => $blocked->getProduct()->getName(),
                    'start' => $blocked->getDateStart()->format('d/m/Y H:i'),
                    'end' => $blocked->getDateEnd()->format('d/m/Y H:i'),
                ]
            ];
        }

        return new JsonResponse($events);
    }
}


