<?php

namespace App\Entity;

use App\Entity\Helpers\TrackedTrait;
use App\Repository\SnipRepository;
use App\Service\SnipContent\SnipContentService;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: SnipRepository::class)]
class Snip
{
    use TrackedTrait;

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $name = null;

    #[ORM\Column]
    private bool $public = false;

    #[ORM\OneToMany(mappedBy: 'snip', targetEntity: SnipContent::class, orphanRemoval: true)]
    private Collection $snipContents;

    #[ORM\OneToOne]
    private ?SnipContent $activeVersion = null;

    #[ORM\Column(length: 255)]
    private ?string $parser = null;

    #[ORM\Column]
    private bool $visible = true;

    #[ORM\Column]
    private bool $archived = false;

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

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

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

    public function getActiveText(): string
    {
        return SnipContentService::rebuildText($this->getActiveVersion());
    }

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

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

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function isPublic(): ?bool
    {
        return $this->public;
    }

    public function setPublic(bool $public): self
    {
        $this->public = $public;

        return $this;
    }

    /**
     * @return Collection<int, SnipContent>
     */
    public function getSnipContents(): Collection
    {
        return $this->snipContents;
    }

    public function addSnipContent(SnipContent $snipContent): self
    {
        if (!$this->snipContents->contains($snipContent)) {
            $this->snipContents->add($snipContent);
            $snipContent->setSnip($this);
        }

        return $this;
    }

    public function removeSnipContent(SnipContent $snipContent): self
    {
        if ($this->snipContents->removeElement($snipContent)) {
            // set the owning side to null (unless already changed)
            if ($snipContent->getSnip() === $this) {
                $snipContent->setSnip(null);
            }
        }

        return $this;
    }

    public function getLatestVersion(): ?SnipContent
    {
        return $this->snipContents->last() ?: null;
    }

    public function getActiveVersion(): ?SnipContent
    {
        return $this->activeVersion;
    }

    public function setActiveVersion(?SnipContent $activeVersion): static
    {
        $this->activeVersion = $activeVersion;

        return $this;
    }

    public function getParser(): ?string
    {
        return $this->parser;
    }

    public function setParser(string $parser): static
    {
        $this->parser = $parser;

        return $this;
    }

    public function isVisible(): ?bool
    {
        return $this->visible;
    }

    public function setVisible(bool $visible): static
    {
        $this->visible = $visible;

        return $this;
    }

    public function isArchived(): ?bool
    {
        return $this->archived;
    }

    public function setArchived(bool $archived): static
    {
        $this->archived = $archived;

        return $this;
    }

    /**
     * @return Collection<int, Tag>
     */
    public function getTags(): Collection
    {
        return $this->tags;
    }

    public function addTag(Tag $tag): static
    {
        if (!$this->tags->contains($tag)) {
            $this->tags->add($tag);
            $tag->addSnip($this);
        }

        return $this;
    }

    public function removeTag(Tag $tag): static
    {
        if ($this->tags->removeElement($tag)) {
            $tag->removeSnip($this);
        }

        return $this;
    }
}
