<?php

namespace AppBundle\Entity\Core;

use AppBundle\Annotation\DefaultField;
use AppBundle\Entity\Organization\Organization;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use AppBundle\Entity\Traits\TimestampableEntity;
use AppBundle\Entity\Traits\CreatorUpdaterEntity;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Entity\Core\TaggableInterface;

/**
 * Lien entre un Tag et une entité taggable
 */
#[ORM\Entity]
#[ORM\Table(name: 'Tagging')]
#[ORM\Index(name: 'resourceId', columns: ['resourceId'])]
#[ORM\Index(name: 'resourceType', columns: ['resourceType'])]
class Tagging
{
    use TimestampableEntity;
    use CreatorUpdaterEntity;
    /**
     * @var int
     */
    #[ORM\Column(type: 'integer')]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    #[Groups(['tag'])]
    protected $id;
    /**
     * @var Organization
     *
     * @DefaultField
     */
    #[ORM\ManyToOne(targetEntity: 'AppBundle\Entity\Organization\Organization')]
    #[ORM\JoinColumn(nullable: false)]
    #[Assert\NotNull]
    #[Groups(['tag'])]
    private $organization;
    /**
     *
     * @var ArrayCollection<Tag>
     */
    #[ORM\OneToMany(targetEntity: 'Tag', mappedBy: 'tagging', cascade: ['persist'], orphanRemoval: true)]
    #[Groups(['tag'])]
    protected $tags;
    /**
     *
     * @var String
     */
    #[ORM\Column(type: 'string', length: 50)]
    protected $resourceType;
    /**
     *
     * @var Integer
     */
    #[ORM\Column(type: 'integer')]
    protected $resourceId;
    /**
     * Constructor
     */
    public function __construct(TaggableInterface $resource = null, Organization $organization = null)
    {
        if (null !== $resource) {
          $this->resourceId = $resource->getTaggableId();
          $this->resourceType = $resource->getTaggableType();
        }
        $this->organization = $organization;
        $this->tags = new ArrayCollection();
    }
    /**
     * Sets id.
     * 
     * @param int $id
     * 
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }
    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Set resourceType
     *
     * @param string $resourceType
     *
     * @return Tagging
     */
    public function setResourceType($resourceType)
    {
        $this->resourceType = $resourceType;

        return $this;
    }
    /**
     * Get resourceType
     *
     * @return string
     */
    public function getResourceType()
    {
        return $this->resourceType;
    }
    /**
     * Set resourceId
     *
     * @param integer $resourceId
     *
     * @return Tagging
     */
    public function setResourceId($resourceId)
    {
        $this->resourceId = $resourceId;

        return $this;
    }
    /**
     * Get resourceId
     *
     * @return integer
     */
    public function getResourceId()
    {
        return $this->resourceId;
    }
    /**
     * Set organization
     *
     * @param \AppBundle\Entity\Organization\Organization $organization
     *
     * @return Tagging
     */
    public function setOrganization(\AppBundle\Entity\Organization\Organization $organization)
    {
        $this->organization = $organization;

        return $this;
    }
    /**
     * Get organization
     *
     * @return \AppBundle\Entity\Organization\Organization
     */
    public function getOrganization()
    {
        return $this->organization;
    }
    /**
     * Add tag
     *
     * @param \AppBundle\Entity\Core\Tag $tag
     *
     * @return Tagging
     */
    public function addTag(\AppBundle\Entity\Core\Tag $tag)
    {
        $tag->setTagging($this);
        $this->tags[] = $tag;

        return $this;
    }
    /**
     * Remove tag
     *
     * @param \AppBundle\Entity\Core\Tag $tag
     */
    public function removeTag(\AppBundle\Entity\Core\Tag $tag)
    {
        $this->tags->removeElement($tag);
    }
    /**
     * Get tags
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getTags()
    {
        return $this->tags;
    }
}
