<?php
declare (strict_types=1);

namespace App\Entity\Education;

use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\Education\EducationCurriculumRepository;

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

/**
 * Curriculum éducatif; composé d'un cycle, d'une année et d'un niveau
 */
#[ApiResource(
    operations: [
        new Get(
            security: 'is_granted(\'ROLE_ADMIN\') and object.getEducation().getEducationCategory().getOrganization().getId() == user.getOrganization().getId()'
        )
    ]
)]
//#[Auditable]
#[ORM\Entity(repositoryClass: EducationCurriculumRepository::class)]
class EducationCurriculum
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    private ?int $id = null;

    #[ORM\ManyToOne(inversedBy: 'educationCurriculums')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private ?EducationNotationConfig $educationNotationConfig;

    #[ORM\ManyToOne(inversedBy: 'educationCurriculums')]
    private Education $education;

    #[ORM\ManyToOne]
    private Cycle $cycle;

    #[ORM\OneToMany(mappedBy: 'educationCurriculum', targetEntity: EducationStudent::class, cascade: ['persist'])]
    private Collection $educationStudent;

    #[ORM\ManyToMany(targetEntity: EducationTiming::class, inversedBy: 'educationCurriculums')]
    #[ORM\JoinColumn(name: 'educationCurriculum_id', referencedColumnName: 'id')]
    #[ORM\InverseJoinColumn(name: 'educationTiming_id', referencedColumnName: 'id')]
    private Collection $educationTimings;

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

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

    public function setEducationNotationConfig(?EducationNotationConfig $educationNotationConfig): self
    {
        $this->educationNotationConfig = $educationNotationConfig;
        return $this;
    }

    public function getEducationNotationConfig(): ?EducationNotationConfig
    {
        return $this->educationNotationConfig;
    }

    public function getEducation(): ?Education
    {
        return $this->education;
    }

    public function setEducation(?Education $education): self
    {
        $this->education = $education;
        return $this;
    }

    public function getCycle(): ?Cycle
    {
        return $this->cycle;
    }

    public function setCycle(?Cycle $cycle): self
    {
        $this->cycle = $cycle;
        return $this;
    }

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

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

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

    /**
     * @return Collection<int, EducationTiming>
     */
    public function getEducationTimings(): Collection
    {
        return $this->educationTimings;
    }

    public function addEducationTiming(EducationTiming $educationTiming): self
    {
        if (!$this->educationTimings->contains($educationTiming)) {
            $this->educationTimings[] = $educationTiming;
        }
        return $this;
    }

    public function removeEducationTiming(EducationTiming $educationTiming): self
    {
        $this->educationTimings->removeElement($educationTiming);
        return $this;
    }
}
