<?php

namespace App\Entity;

use App\Repository\ServiceRdvRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: ServiceRdvRepository::class)]
class ServiceRdv
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(type: Types::DATE_MUTABLE)]
    private ?\DateTimeInterface $start_date = null;

    #[ORM\Column(type: Types::DATE_MUTABLE)]
    private ?\DateTimeInterface $end_date = null;

    #[ORM\Column(type: Types::TIME_MUTABLE)]
    private ?\DateTimeInterface $start_time = null;

    #[ORM\Column(type: Types::TIME_MUTABLE)]
    private ?\DateTimeInterface $end_time = null;

    #[ORM\Column]
    private ?int $duree = null;

    #[ORM\Column(length: 100, nullable: true)]
    private ?string $excludeDay = null;

    #[ORM\Column]
    private ?int $betweenRdv = null;
    
    #[ORM\ManyToOne(inversedBy: 'serviceRdvs')]
    #[ORM\JoinColumn(nullable: false)]
    private ?Service $service = null;

    #[ORM\ManyToOne(inversedBy: 'serviceRdvs')]
    #[ORM\JoinColumn(nullable: false)]
    private ?User $createBy = null;

    #[ORM\Column(nullable: true)]
    private ?array $reservationJson = null;

    #[ORM\Column(type: Types::DATETIME_MUTABLE)]
    private ?\DateTimeInterface $dateAdd = null;

    #[ORM\OneToMany(mappedBy: 'serviceRdv', targetEntity: ServiceRdvReservation::class)]
    private Collection $serviceRdvReservations;

    public function __construct()
    {
        $this->dateAdd = new \DateTime();
        $this->serviceRdvReservations = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getStartDate(): ?\DateTimeInterface
    {
        return $this->start_date;
    }

    public function getStringStartDate(): ?string
    {
        return $this->start_date->format('d/m/Y');
    }

    public function setStartDate(\DateTimeInterface $start_date): static
    {
        $this->start_date = $start_date;

        return $this;
    }

    public function getEndDate(): ?\DateTimeInterface
    {
        return $this->end_date;
    }

    public function getStringEndDate(): ?string
    {
        return $this->end_date->format('d/m/Y');
    }
    
    public function setEndDate(\DateTimeInterface $end_date): static
    {
        $this->end_date = $end_date;

        return $this;
    }

    public function getStartTime(): ?\DateTimeInterface
    {
        return $this->start_time;
    }

    public function getStringStartTime(): ?string
    {
        return $this->start_time->format('H:i');
    }

    public function setStartTime(\DateTimeInterface $start_time): static
    {
        $this->start_time = $start_time;

        return $this;
    }

    public function getEndTime(): ?\DateTimeInterface
    {
        return $this->end_time;
    }

    public function getStringEndTime(): ?string
    {
        return $this->end_time->format('H:i');
    }

    public function setEndTime(\DateTimeInterface $end_time): static
    {
        $this->end_time = $end_time;

        return $this;
    }

    public function getDuree(): ?int
    {
        return $this->duree;
    }

    public function setDuree(int $duree): static
    {
        $this->duree = $duree;

        return $this;
    }

    public function getExcludeDay(): ?string
    {
        return $this->excludeDay;
    }

    public function setExcludeDay(?string $excludeDay): static
    {
        $this->excludeDay = $excludeDay;

        return $this;
    }

    public function getService(): ?Service
    {
        return $this->service;
    }

    public function setService(?Service $service): static
    {
        $this->service = $service;

        return $this;
    }

    public function getCreateBy(): ?User
    {
        return $this->createBy;
    }

    public function setCreateBy(?User $createBy): static
    {
        $this->createBy = $createBy;

        return $this;
    }

    public function getReservationJson(): ?array
    {
        return $this->reservationJson;
    }

    public function setReservationJson(?array $reservationJson): static
    {
        $this->reservationJson = $reservationJson;

        return $this;
    }



    public function getBetweenRdv(): ?int
    {
        return $this->betweenRdv;
    }

    public function setBetweenRdv(int $betweenRdv): static
    {
        $this->betweenRdv = $betweenRdv;

        return $this;
    }

    public function getDateAdd(): ?\DateTimeInterface
    {
        return $this->dateAdd;
    }

    public function setDateAdd(\DateTimeInterface $dateAdd): static
    {
        $this->dateAdd = $dateAdd;

        return $this;
    }

    /**
     * @return Collection<int, ServiceRdvReservation>
     */
    public function getServiceRdvReservations(): Collection
    {
        return $this->serviceRdvReservations;
    }

    public function addServiceRdvReservation(ServiceRdvReservation $serviceRdvReservation): static
    {
        if (!$this->serviceRdvReservations->contains($serviceRdvReservation)) {
            $this->serviceRdvReservations->add($serviceRdvReservation);
            $serviceRdvReservation->setServiceRdv($this);
        }

        return $this;
    }

    public function removeServiceRdvReservation(ServiceRdvReservation $serviceRdvReservation): static
    {
        if ($this->serviceRdvReservations->removeElement($serviceRdvReservation)) {
            // set the owning side to null (unless already changed)
            if ($serviceRdvReservation->getServiceRdv() === $this) {
                $serviceRdvReservation->setServiceRdv(null);
            }
        }

        return $this;
    }


    public function toArray(): ?array
    {
        $Param['id'] = $this->service->getId();
        $Param['startDate'] =  $this->start_date->format('d/m/Y');
        $Param['endDate'] =  $this->end_date->format('d/m/Y');
        $Param['startTime'] =  $this->start_time->format('H:i');
        $Param['endTime'] =  $this->end_time->format('H:i');
        $Param['between'] =  $this->duree;
        $Param ['duree'] = $this->service->getDuree(); 
        $Param['excludeDay'] = $this->excludeDay;
        
        $RendezVous = [];
        foreach($this->serviceRdvReservations as $reservation){
            $RendezVous[$reservation->getDateDebut()->format('d/m/Y')][] = ['debut'=>$reservation->getDateDebut()->format('H:i'), 'fin'=>$reservation->getDateFin()->format('H:i')];
        }
        $Param['arrRendezVous'] = $RendezVous;

        return $Param;
    }


}
