<?php
declare (strict_types=1);

namespace App\Entity\Person;

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;

/**
 * Classe ... qui ...
 */
#[ApiResource(
    operations: [
        new Get(
            security: 'is_granted(\'ROLE_ADMIN\') and object.getOrganization().getId() == user.getOrganization().getId()'
        )
    ]
)]
//#[Auditable]
#[ORM\Entity]
class Commission
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    private ?int $id = null;

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

    #[ORM\OneToMany(mappedBy: 'commission', targetEntity: CommissionMember::class, cascade: ['persist'], orphanRemoval: true)]
    private Collection $commissionMembers;

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

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

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

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

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

    /**
     * @return Collection<int, CommissionMember>
     */
    public function getCommissionMembers(): Collection
    {
        return $this->commissionMembers;
    }

    public function addCommissionMember(CommissionMember $commissionMember): self
    {
        if (!$this->commissionMembers->contains($commissionMember)) {
            $this->commissionMembers[] = $commissionMember;
            $commissionMember->setCommission($this);
        }
        return $this;
    }

    public function removeCommissionMember(CommissionMember $commissionMember): self
    {
        if ($this->commissionMembers->removeElement($commissionMember)) {
            // set the owning side to null (unless already changed)
            if ($commissionMember->getCommission() === $this) {
                $commissionMember->setCommission(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;
    }
}