<?php
declare (strict_types=1);

namespace App\Entity\Education;

use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Attribute\OrganizationDefaultValue;
use App\Entity\Organization\Organization;
use App\Repository\Education\EducationTimingRepository;

//use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Auditable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;

/**
 * Temps d'un enseignement
 */
#[ApiResource(
    operations: [
        new Get(
            security: 'is_granted(\'ROLE_ORGANIZATION_VIEW\') and object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new Put(
            security: 'object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new Delete(
            security: 'object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new GetCollection(
            security: 'is_granted(\'ROLE_ORGANIZATION_VIEW\')'
        ),
        new Post()
    ],
    security: 'is_granted(\'ROLE_ORGANIZATION\')'
)]
//#[Auditable]
#[OrganizationDefaultValue(fieldName: "organization")]
#[ORM\Entity(repositoryClass: EducationTimingRepository::class)]
class EducationTiming
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    private ?int $id = null;

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

    #[ORM\Column]
    private int $timing;

    #[ORM\OneToMany(mappedBy: 'educationTiming', targetEntity: EducationStudent::class, orphanRemoval: true)]
    private Collection $educationStudents;

    #[ORM\ManyToMany(targetEntity: EducationCurriculum::class, mappedBy: 'educationTimings')]
    private Collection $educationCurriculums;

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

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

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

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

    public function setTiming(int $timing): self
    {
        $this->timing = $timing;
        return $this;
    }

    public function getTiming(): int
    {
        return $this->timing;
    }

    /**
     * @return Collection<int, EducationStudent>
     */
    public function getEducationStudents(): Collection
    {
        return $this->educationStudents;
    }

    public function addEducationStudent(EducationStudent $educationStudent): self
    {
        if (!$this->educationStudents->contains($educationStudent)) {
            $this->educationStudents[] = $educationStudent;
            $educationStudent->setEducationTiming($this);
        }
        return $this;
    }

    public function removeEducationStudent(EducationStudent $educationStudent): self
    {
        if ($this->educationStudents->removeElement($educationStudent)) {
            // set the owning side to null (unless already changed)
            if ($educationStudent->getEducationTiming() === $this) {
                $educationStudent->setEducationTiming(null);
            }
        }
        return $this;
    }

    /**
     * @return Collection<int, EducationCurriculum>
     */
    public function getEducationCurriculums(): Collection
    {
        return $this->educationCurriculums;
    }

    public function addEducationCurriculum(EducationCurriculum $educationCurriculum): self
    {
        if (!$this->educationCurriculums->contains($educationCurriculum)) {
            $this->educationCurriculums[] = $educationCurriculum;
            $educationCurriculum->addEducationTiming($this);
        }
        return $this;
    }

    public function removeEducationCurriculum(EducationCurriculum $educationCurriculum): self
    {
        if ($this->educationCurriculums->removeElement($educationCurriculum)) {
            $educationCurriculum->removeEducationTiming($this);
        }
        return $this;
    }
}
