<?php
declare (strict_types=1);

namespace App\Entity\Booking;

use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Entity\Organization\Organization;
//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 OrganizationHoliday, et supprimer l'attribut discr.
 *
 * Classe ... qui ...
 */
#[ApiResource(
    operations: [
        new Get(
            security: 'is_granted(\'ROLE_ADMIN\') and object.getOrganization().getId() == user.getOrganization().getId()'
        )
    ]
)]
//#[Auditable]
#[ORM\Entity]
#[ORM\Table(name: 'Booking')]
class OrganizationHoliday extends AbstractBooking
{
    #[ORM\Column(length: 255, nullable: false)]
    private string $discr = 'organizationholiday';

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

    #[ORM\ManyToOne(inversedBy: 'holidays')]
    #[ORM\JoinColumn(nullable: false)]
    private Organization $organization;

    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, OrganizationHolidayRecur>
     */
    public function getEventRecur(): Collection
    {
        return $this->eventRecur;
    }

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

    public function removeEventRecur(OrganizationHolidayRecur $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 getOrganization(): ?Organization
    {
        return $this->organization;
    }

    public function setOrganization(?Organization $organization): self
    {
        $this->organization = $organization;
        return $this;
    }
}