<?php
declare(strict_types=1);

namespace App\Entity\Booking;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Access\Access;
use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Auditable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @todo : A la suite de la migration, il faut supprimer le nom de la table pour avoir une table PersonHoliday, et supprimer l'attribut discr.
 *
 * Classe ... qui ...
 */
#[Auditable]
#[ORM\Entity]
#[ORM\Table(name: 'Booking')]
#[ApiResource(
    collectionOperations:[],
    itemOperations: [
        "get" => ["security" => "is_granted('ROLE_ADMIN') and object.getAccess().getOrganization().getId() == user.getOrganization().getId()"]
    ]
)]
class PersonHoliday extends AbstractBooking
{
    #[ORM\Column(length: 255, nullable: false)]
    private string $discr = 'personholiday';

    #[ORM\OneToMany(mappedBy: 'event', targetEntity: PersonHolidayRecur::class, cascade: ['persist'], orphanRemoval: true)]
    protected Collection $eventRecur;

    #[ORM\ManyToOne(inversedBy: 'holidays')]
    private Access $access;

    public function __construct()
    {
        $this->eventRecur = new ArrayCollection();
    }

    public function getDiscr(): ?string
    {
        return $this->discr;
    }

    public function setDiscr(string $discr): self
    {
        $this->discr = $discr;

        return $this;
    }

    /**
     * @return Collection<int, PersonHolidayRecur>
     */
    public function getEventRecur(): Collection
    {
        return $this->eventRecur;
    }

    public function addEventRecur(PersonHolidayRecur $eventRecur): self
    {
        if (!$this->eventRecur->contains($eventRecur)) {
            $this->eventRecur[] = $eventRecur;
            $eventRecur->setEvent($this);
        }

        return $this;
    }

    public function removeEventRecur(PersonHolidayRecur $eventRecur): self
    {
        if ($this->eventRecur->removeElement($eventRecur)) {
            // set the owning side to null (unless already changed)
            if ($eventRecur->getEvent() === $this) {
                $eventRecur->setEvent(null);
            }
        }

        return $this;
    }

    public function getAccess(): ?Access
    {
        return $this->access;
    }

    public function setAccess(?Access $access): self
    {
        $this->access = $access;

        return $this;
    }
}