<?php

namespace App\Entity;

use App\Dto\SnipFilterRequest;
use App\Repository\TagRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity(repositoryClass: TagRepository::class)]
#[ORM\UniqueConstraint(name: 'user_tag_unique', columns: ['name', 'user_id'])]
class Tag
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    public ?int $id = null;

    #[ORM\Column(length: 255)]
    #[Assert\NotEqualTo(SnipFilterRequest::TAG_ALL)]
    #[Assert\NotEqualTo(SnipFilterRequest::TAG_NONE)]
    public ?string $name = null;

    #[ORM\ManyToOne]
    #[ORM\JoinColumn(nullable: false)]
    public ?User $user = null;

    /**
     * @var Collection<int, Snip>
     */
    #[ORM\ManyToMany(targetEntity: Snip::class, inversedBy: 'tags')]
    public Collection $snips;

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

    public function __toString(): string
    {
        return $this->name ?? '';
    }

    public function addSnip(Snip $snip): static
    {
        if (!$this->snips->contains($snip)) {
            $this->snips->add($snip);
        }

        return $this;
    }

    public function removeSnip(Snip $snip): static
    {
        $this->snips->removeElement($snip);

        return $this;
    }
}
