<?php

/*
 * @codingStandardsIgnoreFile
 *
 * Auto generated file ignore for Code Sniffer
 */

namespace App\Entity;

use Carbon\Carbon;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Do not check entities with PHPMD.
 *
 * @SuppressWarnings("PHPMD")
 */
#[ORM\Table(name: 'community_news')]
#[ORM\Entity(repositoryClass: \App\Repository\CommunityNewsRepository::class)]
#[ORM\HasLifecycleCallbacks]
class CommunityNews
{
    /**
     * @var string
     */
    #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)]
    private $title;

    /**
     * @var string
     */
    #[ORM\Column(name: 'text', type: 'text', length: 65535, nullable: false)]
    private $text;

    /**
     * @var bool
     */
    #[ORM\Column(name: 'public', type: 'boolean', nullable: false)]
    private $public = false;

    /**
     * @var DateTime
     */
    #[ORM\Column(name: 'created_at', type: 'datetime', nullable: true)]
    private $createdAt;

    /**
     * @var Member
     */
    #[ORM\JoinColumn(name: 'created_by', referencedColumnName: 'id')]
    #[ORM\ManyToOne(targetEntity: Member::class)]
    private $createdBy;

    /**
     * @var DateTime
     */
    #[ORM\Column(name: 'updated_at', type: 'datetime', nullable: true)]
    private $updatedAt;

    /**
     * @var Member
     */
    #[ORM\JoinColumn(name: 'updated_by', referencedColumnName: 'id', nullable: true)]
    #[ORM\ManyToOne(targetEntity: Member::class)]
    private $updatedBy;

    /**
     * @var int
     */
    #[ORM\Column(name: 'id', type: 'integer')]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'IDENTITY')]
    private $id;

    /**
     * One news has many comments.
     */
    #[ORM\OneToMany(targetEntity: CommunityNewsComment::class, mappedBy: 'communityNews', fetch: 'EXTRA_LAZY')]
    #[ORM\OrderBy(['created' => 'DESC'])]
    private $comments;

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

    /**
     * Set title.
     *
     * @param string $title
     *
     * @return CommunityNews
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get title.
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set text.
     *
     * @param string $text
     *
     * @return CommunityNews
     */
    public function setText($text)
    {
        $this->text = $text;

        return $this;
    }

    /**
     * Get text.
     *
     * @return string
     */
    public function getText()
    {
        return $this->text;
    }

    /**
     * Set public.
     *
     * @param bool $public
     *
     * @return CommunityNews
     */
    public function setPublic($public)
    {
        $this->public = $public;

        return $this;
    }

    /**
     * Get public.
     *
     * @return bool
     */
    public function getPublic()
    {
        return $this->public;
    }

    /**
     * Set createdAt.
     *
     * @param DateTime $createdAt
     *
     * @return CommunityNews
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    /**
     * Get createdAt.
     *
     * @return Carbon
     */
    public function getCreatedAt()
    {
        return Carbon::instance($this->createdAt);
    }

    /**
     * Set createdBy.
     *
     * @return CommunityNews
     */
    public function setCreatedBy(Member $createdBy)
    {
        $this->createdBy = $createdBy;

        return $this;
    }

    /**
     * Get createdBy.
     *
     * @return Member
     */
    public function getCreatedBy()
    {
        return $this->createdBy;
    }

    /**
     * Set updatedAt.
     *
     * @param DateTime $updatedAt
     *
     * @return CommunityNews
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    /**
     * Get updatedAt.
     *
     * @return Carbon
     */
    public function getUpdatedAt()
    {
        return Carbon::instance($this->updatedAt);
    }

    /**
     * Set updatedBy.
     *
     * @return CommunityNews
     */
    public function setUpdatedBy(Member $updatedBy)
    {
        $this->updatedBy = $updatedBy;

        return $this;
    }

    /**
     * Get updatedBy.
     *
     * @return Member
     */
    public function getUpdatedBy()
    {
        return $this->updatedBy;
    }

    /**
     * Get id.
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Get comments.
     *
     * @return ArrayCollection
     */
    public function getComments()
    {
        return $this->comments;
    }

    /**
     * Triggered on insert.
     */
    #[ORM\PrePersist]
    public function onPrePersist()
    {
        $this->createdAt = new DateTime('now');
    }

    /**
     * Triggered on update.
     */
    #[ORM\PreUpdate]
    public function onPreUpdate()
    {
        $this->updatedAt = new DateTime('now');
    }

    public function addComment(CommunityNewsComment $comment): self
    {
        if (!$this->comments->contains($comment)) {
            $this->comments[] = $comment;
            $comment->setCommunityNews($this);
        }

        return $this;
    }

    public function removeComment(CommunityNewsComment $comment): self
    {
        if ($this->comments->contains($comment)) {
            $this->comments->removeElement($comment);
            // set the owning side to null (unless already changed)
            if ($comment->getCommunityNews() === $this) {
                $comment->setCommunityNews(null);
            }
        }

        return $this;
    }
}
