<?php
declare (strict_types=1);

namespace App\Entity\Organization;

use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Entity\Core\Categories;
use App\Entity\Core\File;
use App\Entity\Core\Tagg;
use App\Entity\Person\PersonActivity;

//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 Activity
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    private ?int $id = null;

    #[ORM\ManyToOne(cascade: ['persist'], inversedBy: 'activityLogos')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private File $logo;

    #[ORM\ManyToOne(cascade: ['persist'], inversedBy: 'activityImages')]
    #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    private File $imageActivity;

    #[ORM\ManyToOne]
    private ActivityType $activityType;

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

    #[ORM\ManyToMany(targetEntity: Categories::class, cascade: ['persist'])]
    private Collection $categories;

    #[ORM\OneToMany(mappedBy: 'activity', targetEntity: PersonActivity::class)]
    private Collection $personActivities;

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

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

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

    public function getLogo(): ?File
    {
        return $this->logo;
    }

    public function setLogo(?File $logo): self
    {
        $this->logo = $logo;
        return $this;
    }

    public function getImageActivity(): ?File
    {
        return $this->imageActivity;
    }

    public function setImageActivity(?File $imageActivity): self
    {
        $this->imageActivity = $imageActivity;
        return $this;
    }

    public function getActivityType(): ?ActivityType
    {
        return $this->activityType;
    }

    public function setActivityType(?ActivityType $activityType): self
    {
        $this->activityType = $activityType;
        return $this;
    }

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

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

    /**
     * @return Collection<int, Categories>
     */
    public function getCategories(): Collection
    {
        return $this->categories;
    }

    public function addCategory(Categories $category): self
    {
        if (!$this->categories->contains($category)) {
            $this->categories[] = $category;
        }
        return $this;
    }

    public function removeCategory(Categories $category): self
    {
        $this->categories->removeElement($category);
        return $this;
    }

    /**
     * @return Collection<int, PersonActivity>
     */
    public function getPersonActivities(): Collection
    {
        return $this->personActivities;
    }

    public function addPersonActivity(PersonActivity $personActivity): self
    {
        if (!$this->personActivities->contains($personActivity)) {
            $this->personActivities[] = $personActivity;
            $personActivity->setActivity($this);
        }
        return $this;
    }

    public function removePersonActivity(PersonActivity $personActivity): self
    {
        if ($this->personActivities->removeElement($personActivity)) {
            // set the owning side to null (unless already changed)
            if ($personActivity->getActivity() === $this) {
                $personActivity->setActivity(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;
    }
}