<?php
declare (strict_types=1);

namespace App\Entity\Education;

use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Entity\Access\Access;
use App\Entity\Core\Tagg;

//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.getAccess().getOrganization().getId() == user.getOrganization().getId()'
        )
    ]
)]
//#[Auditable]
#[ORM\Entity]
class EducationStudent
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    private ?int $id = null;

    #[ORM\ManyToOne(cascade: ['persist'], inversedBy: 'educationStudent')]
    #[ORM\JoinColumn(nullable: false)]
    private Access $access;

    #[ORM\ManyToOne(inversedBy: 'educationStudent')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private EducationCurriculum $educationCurriculum;

    #[ORM\ManyToMany(targetEntity: Access::class, inversedBy: 'educationStudentByTeacher')]
    #[ORM\JoinTable(name: 'educationstudent_teacher')]
    private Collection $teachers;

    #[ORM\OneToMany(mappedBy: 'educationStudent', targetEntity: EducationNotation::class, cascade: ['persist'], orphanRemoval: true)]
    private Collection $educationNotations;

    #[ORM\ManyToOne(inversedBy: 'referencedEducationStudent')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private EducationStudent $educationStudentLastYear;

    #[ORM\OneToMany(mappedBy: 'educationStudentLastYear', targetEntity: EducationStudent::class)]
    protected Collection $referencedEducationStudent;

    #[ORM\ManyToMany(targetEntity: Tagg::class, inversedBy: 'educationStudents', cascade: ['persist'])]
    #[ORM\JoinTable(name: 'tag_educationStudent')]
    #[ORM\JoinColumn(name: 'educationStudent_id', referencedColumnName: 'id')]
    #[ORM\InverseJoinColumn(name: 'tag_id', referencedColumnName: 'id')]
    private Collection $tags;

    #[ORM\ManyToOne(inversedBy: 'educationStudents')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private EducationTiming $educationTiming;

    public function __construct()
    {
        $this->teachers = new ArrayCollection();
        $this->educationNotations = new ArrayCollection();
        $this->referencedEducationStudent = new ArrayCollection();
        $this->tags = new ArrayCollection();
    }

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

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

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

    public function getEducationCurriculum(): ?EducationCurriculum
    {
        return $this->educationCurriculum;
    }

    public function setEducationCurriculum(?EducationCurriculum $educationCurriculum): self
    {
        $this->educationCurriculum = $educationCurriculum;
        return $this;
    }

    /**
     * @return Collection<int, Access>
     */
    public function getTeachers(): Collection
    {
        return $this->teachers;
    }

    public function addTeacher(Access $teacher): self
    {
        if (!$this->teachers->contains($teacher)) {
            $this->teachers[] = $teacher;
        }
        return $this;
    }

    public function removeTeacher(Access $teacher): self
    {
        $this->teachers->removeElement($teacher);
        return $this;
    }

    /**
     * @return Collection<int, EducationNotation>
     */
    public function getEducationNotations(): Collection
    {
        return $this->educationNotations;
    }

    public function addEducationNotation(EducationNotation $educationNotation): self
    {
        if (!$this->educationNotations->contains($educationNotation)) {
            $this->educationNotations[] = $educationNotation;
            $educationNotation->setEducationStudent($this);
        }
        return $this;
    }

    public function removeEducationNotation(EducationNotation $educationNotation): self
    {
        if ($this->educationNotations->removeElement($educationNotation)) {
            // set the owning side to null (unless already changed)
            if ($educationNotation->getEducationStudent() === $this) {
                $educationNotation->setEducationStudent(null);
            }
        }
        return $this;
    }

    public function getEducationStudentLastYear(): ?self
    {
        return $this->educationStudentLastYear;
    }

    public function setEducationStudentLastYear(?self $educationStudentLastYear): self
    {
        $this->educationStudentLastYear = $educationStudentLastYear;
        return $this;
    }

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

    public function addReferencedEducationStudent(EducationStudent $referencedEducationStudent): self
    {
        if (!$this->referencedEducationStudent->contains($referencedEducationStudent)) {
            $this->referencedEducationStudent[] = $referencedEducationStudent;
            $referencedEducationStudent->setEducationStudentLastYear($this);
        }
        return $this;
    }

    public function removeReferencedEducationStudent(EducationStudent $referencedEducationStudent): self
    {
        if ($this->referencedEducationStudent->removeElement($referencedEducationStudent)) {
            // set the owning side to null (unless already changed)
            if ($referencedEducationStudent->getEducationStudentLastYear() === $this) {
                $referencedEducationStudent->setEducationStudentLastYear(null);
            }
        }
        return $this;
    }

    /**
     * @return Collection<int, Tagg>
     */
    public function getTags(): Collection
    {
        return $this->tags;
    }

    public function addTag(Tagg $tag): self
    {
        if (!$this->tags->contains($tag)) {
            $this->tags[] = $tag;
        }
        return $this;
    }

    public function removeTag(Tagg $tag): self
    {
        $this->tags->removeElement($tag);
        return $this;
    }

    public function getEducationTiming(): ?EducationTiming
    {
        return $this->educationTiming;
    }

    public function setEducationTiming(?EducationTiming $educationTiming): self
    {
        $this->educationTiming = $educationTiming;
        return $this;
    }
}