<?php
declare (strict_types=1);

namespace App\Entity\Booking;

use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Entity\Access\Access;
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;

/**
 * Classe ... qui ...
 */
#[ApiResource(
    operations: [
        new Get(
            security: 'is_granted(\'ROLE_ADMIN\') and object.getOrganization().getId() == user.getOrganization().getId()'
        )
    ]
)]
//#[Auditable]
#[ORM\Entity]
class Attendance
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    private ?int $id = null;

    #[ORM\ManyToOne(inversedBy: 'attendances')]
    private Organization $organization;

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

    #[ORM\ManyToOne(inversedBy: 'attendanceReplacements')]
    private Access $replacement;

    #[ORM\OneToMany(mappedBy: 'attendance', targetEntity: AttendanceBooking::class, cascade: ['persist'], orphanRemoval: true)]
    #[ORM\JoinColumn(nullable: false)]
    private Collection $attendanceBooking;

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

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

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

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

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

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

    public function getReplacement(): ?Access
    {
        return $this->replacement;
    }

    public function setReplacement(?Access $replacement): self
    {
        $this->replacement = $replacement;
        return $this;
    }

    /**
     * @return Collection<int, AttendanceBooking>
     */
    public function getAttendanceBooking(): Collection
    {
        return $this->attendanceBooking;
    }

    public function addAttendanceBooking(AttendanceBooking $attendanceBooking): self
    {
        if (!$this->attendanceBooking->contains($attendanceBooking)) {
            $this->attendanceBooking[] = $attendanceBooking;
            $attendanceBooking->setAttendance($this);
        }
        return $this;
    }

    public function removeAttendanceBooking(AttendanceBooking $attendanceBooking): self
    {
        if ($this->attendanceBooking->removeElement($attendanceBooking)) {
            // set the owning side to null (unless already changed)
            if ($attendanceBooking->getAttendance() === $this) {
                $attendanceBooking->setAttendance(null);
            }
        }
        return $this;
    }
}