<?php

namespace App\Entity\Campaign;

use App\Entity\Campaign;
use App\Entity\Campaign\Keyword\Check;
use App\Entity\Campaign\Keyword\Tag;
use App\Entity\Traits\TimestampableTrait;
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()
 * @ORM\Table(name="campaign_keyword")
 * @ORM\EntityListeners({"App\EntityListener\SemrushKeywordListener"})
 */
class Keyword
{
    use TimestampableTrait;

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank()
     */
    private $title;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Campaign\Keyword\Tag", inversedBy="keywords")
     * @ORM\JoinTable(name="campaign_keyword_keyword_tag")
     */
    private $tags;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Campaign")
     * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
     * @Assert\NotBlank()
     */
    private $campaign;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Campaign\Keyword\Check", mappedBy="keyword")
     * @ORM\OrderBy({"day" = "DESC"})
     */
    private $checks;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\Campaign\Keyword\Check")
     * @ORM\JoinColumn(onDelete="SET NULL")
     */
    private $latestCheck;

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

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

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

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

    public function addTag(Tag $tag): self
    {
        if (!$this->tags->contains($tag)) {
            $this->tags[] = $tag;
        }

        return $this;
    }

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

        return $this;
    }

    public function getCampaign(): ?Campaign
    {
        return $this->campaign;
    }

    public function setCampaign(?Campaign $campaign): self
    {
        $this->campaign = $campaign;

        return $this;
    }

    /**
     * @return Collection
     */
    public function getChecks(): Collection
    {
        return $this->checks;
    }

    public function addCheck(Check $check): self
    {
        if (!$this->checks->contains($check)) {
            $this->checks[] = $check;
            $check->setKeyword($this);
        }

        return $this;
    }

    public function removeCheck(Check $check): self
    {
        if ($this->checks->contains($check)) {
            $this->checks->removeElement($check);
            // set the owning side to null (unless already changed)
            if ($check->getKeyword() === $this) {
                $check->setKeyword(null);
            }
        }

        return $this;
    }

    public function getMostRecentCheck()
    {
        return $this->getLatestCheck();
    }

    public function getLatestCheck(): ?Check
    {
        return $this->latestCheck;
    }

    public function setLatestCheck(?Check $latestCheck): self
    {
        $this->latestCheck = $latestCheck;

        return $this;
    }
}
