<?php
declare (strict_types=1);

namespace App\Entity\Education;

use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Attribute\OrganizationDefaultValue;
use App\Entity\Access\Access;
use App\Repository\Education\EducationNotationConfigRepository;
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;
use JetBrains\PhpStorm\Pure;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Configuration des grilles d'évaluation
 */
#[ApiResource(
    operations: [
        new Get(
            security: 'is_granted(\'ROLE_PEDAGOGICS_ADMINISTRATION_VIEW\') and object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new Put(
            security: 'object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new GetCollection(
            security: 'is_granted(\'ROLE_PEDAGOGICS_ADMINISTRATION_VIEW\')'
        )
    ],
    security: 'is_granted(\'ROLE_PEDAGOGICS_ADMINISTRATION\')'
)]
//#[Auditable]
#[ORM\Entity(repositoryClass: EducationNotationConfigRepository::class)]
#[OrganizationDefaultValue(fieldName: "organization")]
class EducationNotationConfig
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    private ?int $id = null;

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

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

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

    #[ORM\Column(type: 'text', nullable: true)]
    private ?string $description;

    #[ORM\Column(options: ['default' => 1])]
    #[Assert\Range(notInRangeMessage: 'between_{{ min }}_and_{{ max }}', min: 1, max: 10)]
    private int $coefficient = 1;

    #[ORM\OneToMany(mappedBy: 'educationNotationConfig', targetEntity: Access::class)]
    private Collection $teachers;

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

    #[ORM\OneToMany(mappedBy: 'educationNotationConfig', targetEntity: EducationNotationCriteriaConfig::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
    private Collection $educationNotationCriteriaConfigs;

    #[Pure]
    public function __construct()
    {
        $this->teachers = new ArrayCollection();
        $this->educationCurriculums = new ArrayCollection();
        $this->educationNotationCriteriaConfigs = 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 setLabel(string $label): self
    {
        $this->label = $label;
        return $this;
    }

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

    public function setIsActive(bool $isActive): self
    {
        $this->isActive = $isActive;
        return $this;
    }

    public function getIsActive(): bool
    {
        return $this->isActive;
    }

    public function setDescription(?string $description): self
    {
        $this->description = $description;
        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setCoefficient(?int $coefficient): self
    {
        if ($coefficient === null) {
            $coefficient = 1;
        }
        $this->coefficient = $coefficient;
        return $this;
    }

    public function getCoefficient(): int
    {
        return $this->coefficient;
    }

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

    public function removeTeacher(Access $teacher): self
    {
        if ($this->teachers->removeElement($teacher)) {
            // set the owning side to null (unless already changed)
            if ($teacher->getEducationNotationConfig() === $this) {
                $teacher->setEducationNotationConfig(null);
            }
        }
        return $this;
    }

    public function getTeachers(): Collection
    {
        return $this->teachers;
    }

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

    public function removeEducationCurriculum(EducationCurriculum $educationCurriculums): self
    {
        if ($this->educationCurriculums->removeElement($educationCurriculums)) {
            // set the owning side to null (unless already changed)
            if ($educationCurriculums->getEducationNotationConfig() === $this) {
                $educationCurriculums->setEducationNotationConfig(null);
            }
        }
        return $this;
    }

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

    /**
     * @return Collection<int, EducationNotationCriteriaConfig>
     */
    public function getEducationNotationCriteriaConfigs(): Collection
    {
        return $this->educationNotationCriteriaConfigs;
    }

    public function addEducationNotationCriteriaConfig(EducationNotationCriteriaConfig $educationNotationCriteriaConfig): self
    {
        if (!$this->educationNotationCriteriaConfigs->contains($educationNotationCriteriaConfig)) {
            $this->educationNotationCriteriaConfigs[] = $educationNotationCriteriaConfig;
            $educationNotationCriteriaConfig->setEducationNotationConfig($this);
        }
        return $this;
    }

    public function removeEducationNotationCriteriaConfig(EducationNotationCriteriaConfig $educationNotationCriteriaConfig): self
    {
        if ($this->educationNotationCriteriaConfigs->removeElement($educationNotationCriteriaConfig)) {
            // set the owning side to null (unless already changed)
            if ($educationNotationCriteriaConfig->getEducationNotationConfig() === $this) {
                $educationNotationCriteriaConfig->setEducationNotationConfig(null);
            }
        }
        return $this;
    }
}
