<?php

declare(strict_types=1);

namespace App\Entity\Education;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use App\Entity\Organization\Organization;
use App\Enum\Education\CycleEnum;
use App\Repository\Education\CycleRepository;
use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Auditable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Enum des cycles éducatifs, utilisés par les EducationCurriculum
 * NB: le nombre de cycles est fixé à 6, mais chaque Organization peut en modifier le label.
 *
 * Security :
 *
 *   * @see App\Doctrine\Education\CurrentCycleExtension
 */
#[ApiResource(
    operations: [
        new Get(
            security: 'is_granted(\'ROLE_ORGANIZATION_VIEW\') and object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new Patch(
            security: 'is_granted(\'ROLE_ORGANIZATION\') and object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new GetCollection(
            security: 'is_granted(\'ROLE_ORGANIZATION_VIEW\')'
        ),
    ]
)]
#[Auditable]
#[ORM\Entity(repositoryClass: CycleRepository::class)]
class Cycle
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    private ?int $id = null;

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

    #[ORM\Column(name: '`order`', nullable: true)]
    private ?int $order = null;

    #[ORM\Column(length: 255)]
    private string $label;

    #[ORM\Column(length: 50, enumType: CycleEnum::class)]
    private CycleEnum $cycleEnum;

    #[ORM\Column(options: ['default' => false])]
    private bool $isSystem = false;

    /** @var Collection<int, CycleByEducation> */
    #[ORM\OneToMany(targetEntity: CycleByEducation::class, mappedBy: 'cycle', orphanRemoval: true)]
    private Collection $cycleByEducations;

    /** @var Collection<int, EducationCurriculum> */
    #[ORM\OneToMany(targetEntity: EducationCurriculum::class, mappedBy: 'cycle', cascade: [], orphanRemoval: false)]
    protected Collection $educationCurriculums;

    public function __construct()
    {
        $this->cycleByEducations = 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 setOrder(?int $order): self
    {
        $this->order = $order;

        return $this;
    }

    public function getOrder(): ?int
    {
        return $this->order;
    }

    public function setLabel(string $label): self
    {
        $this->label = $label;

        return $this;
    }

    public function getLabel(): string
    {
        return $this->label;
    }

    public function setCycleEnum(CycleEnum $cycleEnum): self
    {
        $this->cycleEnum = $cycleEnum;

        return $this;
    }

    public function getCycleEnum(): CycleEnum
    {
        return $this->cycleEnum;
    }

    public function setIsSystem(bool $isSystem): self
    {
        $this->isSystem = $isSystem;

        return $this;
    }

    public function getIsSystem(): bool
    {
        return $this->isSystem;
    }

    /**
     * @return Collection<int, CycleByEducation>
     */
    public function getCycleByEducations(): Collection
    {
        return $this->cycleByEducations;
    }

    public function addCycleByEducation(CycleByEducation $cycleByEducation): self
    {
        if (!$this->cycleByEducations->contains($cycleByEducation)) {
            $this->cycleByEducations[] = $cycleByEducation;
            $cycleByEducation->setCycle($this);
        }

        return $this;
    }

    public function removeCycleByEducation(CycleByEducation $cycleByEducation): self
    {
        if ($this->cycleByEducations->removeElement($cycleByEducation)) {
            // set the owning side to null (unless already changed)
            if ($cycleByEducation->getCycle() === $this) {
                $cycleByEducation->setCycle(null);
            }
        }

        return $this;
    }

    public function getEducationCurriculums(): Collection
    {
        return $this->educationCurriculums;
    }

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

        return $this;
    }

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

        return $this;
    }
}
