<?php
declare (strict_types=1);

namespace App\Entity\Product;

use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Entity\Core\Tagg;
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 App\Entity\Billing\AccessIntangible;
use App\Entity\Education\EducationCurriculum;

/**
 * @todo : A la suite de la migration, il faut supprimer le nom de la table pour avoir une table Intangible, et supprimer l'attribut discr.
 * @todo : migration table tag_product
 * Classe ... qui ...
 */
#[ApiResource(
    operations: [
        new Get(
            security: 'is_granted(\'ROLE_ADMIN\') and object.getOrganization().getId() == user.getOrganization().getId()'
        )
    ]
)]
//#[Auditable]
#[ORM\Entity]
#[ORM\Table(name: 'Product')]
class Intangible extends AbstractProduct
{
    #[ORM\Column(length: 255, nullable: false)]
    private string $discr = 'intangible';

    #[ORM\ManyToOne(inversedBy: 'intangibles')]
    protected Organization $organization;

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

    #[ORM\OneToMany(mappedBy: 'intangible', targetEntity: AccessIntangible::class, cascade: ['persist'], orphanRemoval: true)]
    private Collection $accessIntangibles;

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

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

    public function getDiscr(): ?string
    {
        return $this->discr;
    }

    public function setDiscr(string $discr): self
    {
        $this->discr = $discr;
        return $this;
    }

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

    public function setOrganization(?Organization $organization): self
    {
        $this->organization = $organization;
        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;
        }
        return $this;
    }

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

    /**
     * @return Collection<int, AccessIntangible>
     */
    public function getAccessIntangibles(): Collection
    {
        return $this->accessIntangibles;
    }

    public function addAccessIntangible(AccessIntangible $accessIntangible): self
    {
        if (!$this->accessIntangibles->contains($accessIntangible)) {
            $this->accessIntangibles[] = $accessIntangible;
            $accessIntangible->setIntangible($this);
        }
        return $this;
    }

    public function removeAccessIntangible(AccessIntangible $accessIntangible): self
    {
        if ($this->accessIntangibles->removeElement($accessIntangible)) {
            // set the owning side to null (unless already changed)
            if ($accessIntangible->getIntangible() === $this) {
                $accessIntangible->setIntangible(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;
    }
}