<?php

namespace App\Entity;

use App\Repository\BlogRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JetBrains\PhpStorm\Pure;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\String\Slugger\SluggerInterface;

/**
 * @ORM\Entity(repositoryClass=BlogRepository::class)
 * @ORM\HasLifecycleCallbacks()
 * @UniqueEntity("slug")
 */
class Blog
{
	/**
	 * @ORM\Id
	 * @ORM\GeneratedValue
	 * @ORM\Column(type="integer")
	 */
	private $id;
	
	/**
	 * @ORM\Column(type="string", length=255)
	 */
	private ?string $title;
	
	/**
	 * @ORM\Column(type="text", nullable=true)
	 */
	private ?string $teaser;
	
	/**
	 * @ORM\Column(type="string", length=255, nullable=true)
	 */
	private ?string $teaserImage;
	
	/**
	 * @ORM\Column(type="text")
	 */
	private ?string $content;
	
	/**
	 * @ORM\ManyToOne(targetEntity=User::class, inversedBy="blogs")
	 * @ORM\JoinColumn(nullable=false)
	 */
	private ?User $author;
	
	/**
	 * @ORM\ManyToMany(targetEntity=Section::class, inversedBy="blogs")
	 */
	private $section;
	
	/**
	 * @ORM\Column(type="datetime")
	 */
	private ?\DateTimeInterface $createdAt;
	
	/**
	 * @ORM\Column(type="datetime", nullable=true)
	 */
	private ?\DateTimeInterface $editedAt;
	
	/**
	 * @ORM\ManyToOne(targetEntity=User::class)
	 */
	private ?User $editedBy;
	
	/**
	 * @ORM\Column(type="string", length=255, nullable=true)
	 */
	private ?string $editReason;
	
	/**
	 * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="blog")
	 */
	private $comments;
	
	/**
	 * @ORM\Column(type="string", length=255, unique=true)
	 */
	private $slug;
	
	#[Pure]
	public function __construct()
	{
		$this->section = new ArrayCollection();
		$this->comments = new ArrayCollection();
	}
	
	/**
	 * @return null|string
	 */
	public function __toString()
	{
		return $this->title;
	}
	
	
	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;
	}
	
	public function getTeaser(): ?string
	{
		return $this->teaser;
	}
	
	public function setTeaser(?string $teaser): self
	{
		$this->teaser = $teaser;
		
		return $this;
	}
	
	public function getTeaserImage(): ?string
	{
		return $this->teaserImage;
	}
	
	public function setTeaserImage(?string $teaserImage): self
	{
		$this->teaserImage = $teaserImage;
		
		return $this;
	}
	
	public function getContent(): ?string
	{
		return $this->content;
	}
	
	public function setContent(string $content): self
	{
		$this->content = $content;
		
		return $this;
	}
	
	public function getAuthor(): ?User
	{
		return $this->author;
	}
	
	public function setAuthor(?User $author): self
	{
		$this->author = $author;
		
		return $this;
	}
	
	/**
	 * @return Collection|Section[]
	 */
	public function getSection(): Collection
	{
		return $this->section;
	}
	
	public function addSection(Section $section): self
	{
		if (!$this->section->contains($section)) {
			$this->section[] = $section;
		}
		
		return $this;
	}
	
	public function removeSection(Section $section): self
	{
		$this->section->removeElement($section);
		
		return $this;
	}
	
	public function getCreatedAt(): ?\DateTimeInterface
	{
		return $this->createdAt;
	}
	
	public function setCreatedAt(\DateTimeInterface $createdAt): self
	{
		$this->createdAt = $createdAt;
		
		return $this;
	}
	
	public function getEditedAt(): ?\DateTimeInterface
	{
		return $this->editedAt;
	}
	
	public function setEditedAt(?\DateTimeInterface $editedAt): self
	{
		$this->editedAt = $editedAt;
		
		return $this;
	}
	
	public function getEditedBy(): ?User
	{
		return $this->editedBy;
	}
	
	public function setEditedBy(?User $editedBy): self
	{
		$this->editedBy = $editedBy;
		
		return $this;
	}
	
	public function getEditReason(): ?string
	{
		return $this->editReason;
	}
	
	public function setEditReason(?string $editReason): self
	{
		$this->editReason = $editReason;
		
		return $this;
	}
	
	/**
	 * @return Collection|Comment[]
	 */
	public function getComments(): Collection
	{
		return $this->comments;
	}
	
	public function addComment(Comment $comment): self
	{
		if (!$this->comments->contains($comment)) {
			$this->comments[] = $comment;
			$comment->setBlog($this);
		}
		
		return $this;
	}
	
	public function removeComment(Comment $comment): self
	{
		if ($this->comments->removeElement($comment)) {
			// set the owning side to null (unless already changed)
			if ($comment->getBlog() === $this) {
				$comment->setBlog(null);
			}
		}
		
		return $this;
	}
	
	public function getSlug(): ?string
	{
		return $this->slug;
	}
	
	public function setSlug(string $slug): self
	{
		$this->slug = $slug;
		return $this;
	}
	
	
	/**
	 * @ORM\PrePersist
	 */
	public function onPrePersist()
	{
		$this->createdAt = new \DateTime();
	}
	
	/**
	 * @param SluggerInterface $slugger
	 */
	public function computeSlug(SluggerInterface $slugger)
	{
		$this->slug = $slugger->slug($this->title);
	}
	
}
