<?php

namespace App\Entity\Domain;

use App\Entity\Domain;
use App\Entity\Domain\Stat\Check;
use App\Entity\Stat as StatEntity;
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="domain_stat")
 * @ORM\EntityListeners({"App\EntityListener\StatListener"})
 */
class Stat
{
    use TimestampableTrait;

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

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

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $value;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Stat")
     * @ORM\JoinColumn(nullable=false)
     * @Assert\NotBlank()
     */
    private $stat;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Domain\Stat\Check", mappedBy="stat", cascade={"persist", "remove"}, orphanRemoval=true)
     * @ORM\OrderBy({"createdAt" = "DESC"})
     */
    private $checks;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $checkedAt;

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

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

    public function getDomain(): ?Domain
    {
        return $this->domain;
    }

    public function setDomain(?Domain $domain): self
    {
        $this->domain = $domain;

        return $this;
    }

    public function getValue(): ?int
    {
        return $this->value;
    }

    public function setValue(?int $value): self
    {
        $this->value = $value;

        return $this;
    }

    public function getStat(): ?StatEntity
    {
        return $this->stat;
    }

    public function setStat(?StatEntity $stat): self
    {
        $this->stat = $stat;

        return $this;
    }

    public function getCheckedAt(): ?\DateTimeInterface
    {
        return $this->checkedAt;
    }

    public function setCheckedAt(?\DateTimeInterface $checkedAt): self
    {
        $this->checkedAt = $checkedAt;

        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->setStat($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->getStat() === $this) {
                $check->setStat(null);
            }
        }

        return $this;
    }

    public function getDataType(): string
    {
        return 'domain_stat';
    }
}
