<?php

declare(strict_types=1);

namespace App\Entity\Booking;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use App\Attribute\OrganizationDefaultValue;
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;

/**
 * Motif d'absence ou de retard.
 *
 * Security :
 *
 *   @see \App\Doctrine\Education\AttendanceBookingReasonExtension.php
 */
#[ApiResource(
    operations: [
        new Get(
            security: 'is_granted(\'ROLE_ORGANIZATION_VIEW\') and object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new Put(
            security: 'is_granted(\'ROLE_ORGANIZATION\') and object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new Patch(
            security: 'is_granted(\'ROLE_ORGANIZATION\') and object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new Delete(
            security: 'is_granted(\'ROLE_ORGANIZATION\') and object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new GetCollection(
            security: 'is_granted(\'ROLE_ORGANIZATION_VIEW\')'
        ),
        new Post(
            security: 'is_granted(\'ROLE_ORGANIZATION\')'
        )]
)]
#[Auditable]
#[ORM\Table(name: 'AttendanceBookingReason')]
#[OrganizationDefaultValue(fieldName: 'organization')]
#[ORM\Entity]
class AttendanceBookingReason
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    private ?int $id = null;

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

    #[ORM\Column(length: 255, nullable: false)]
    private string $reason;

    /**
     * Pas de cascade remove ici, les absences / présences existent indépendamment de ce motif.
     *
     * @var Collection<int, AttendanceBooking>
     */
    #[ORM\OneToMany(targetEntity: AttendanceBooking::class, mappedBy: 'reason', cascade: ['persist'])]
    private Collection $attendanceBookings;

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

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

    public function setId(?int $id): self
    {
        $this->id = $id;

        return $this;
    }

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

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

        return $this;
    }

    public function getReason(): string
    {
        return $this->reason;
    }

    public function setReason(string $reason): self
    {
        $this->reason = $reason;

        return $this;
    }

    public function getAttendanceBookings(): Collection
    {
        return $this->attendanceBookings;
    }

    public function addAttendanceBookings(AttendanceBooking $attendanceBooking): self
    {
        if (!$this->attendanceBookings->contains($attendanceBooking)) {
            $this->attendanceBookings[] = $attendanceBooking;
            $attendanceBooking->setReason($this);
        }

        return $this;
    }

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

        return $this;
    }
}
