<?php

namespace App\Twig\Components;

use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use App\Entity\Event;
use App\Entity\Period;
use App\Entity\Space;
use App\Entity\Slot;
use App\Service\DateTimeHelper;
use App\Service\SlotFinder;
use App\Repository\SlotRepository;

#[AsTwigComponent]
class Planning
{
    public ?string $pathEmptySlot = null;
    public ?string $pathFullSlot = null;
    public ?string $pathLockedSlot = null;
    public Event $event;
    public bool $onlyUserSlot = false;
    public bool $onlyEmptyParty = false;
    public bool $displayLocked = false;
    public bool $displayUnvalidates = false;

    private DateTimeHelper $dateTimeHelper;
    private SlotRepository $repository;
    private SlotFinder $finder;

    public function __construct(DateTimeHelper $dateTimeHelper, SlotRepository $repository)
    {
        $this->dateTimeHelper = $dateTimeHelper;
        $this->repository = $repository;
    }

    public function mount(Event $event): void
    {
        $this->event = $event;
        $this->finder = new SlotFinder($this->event->getSlots());
    }

    public function getDateOrdered(Period $period): array
    {
        $dates = [];
        $cursorDate = $this->dateTimeHelper->roundUpToNextSlot($period->getStartOn());

        while ($cursorDate < $period->getEndOn()) {
            $dates[] = clone $cursorDate;
            $cursorDate->add(new \DateInterval('PT30M'));
        }

        return $dates;
    }

    public function getThisSlotInfo(\DateTimeInterface $dateRef, Space $space, Period $period): ?Slot
    {
        $found = $this->finder->find($dateRef, $space, $period);
        return $found;
    }

    // Si tu veux garder la recherche SQL (moins efficace)
    public function getThisSlot(\DateTimeInterface $dateRef, Space $space, Period $period): ?Slot
    {
        return $this->repository->findASlot($dateRef, $space, $period);
    }
}