<?php

/*
 * This file is part of the VitiCore 2.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\News\Model;

use CpCreation\VitiCore\Behavior\HasDisable;
use CpCreation\VitiCore\Behavior\HasFile;
use CpCreation\VitiCore\Behavior\HasTimestamp;
use CpCreation\VitiCore\Behavior\HasTranslatable;
use CpCreation\VitiCore\Behavior\HasVideo;
use CpCreation\VitiCore\Behavior\Impl\Disable;
use CpCreation\VitiCore\Behavior\Impl\File;
use CpCreation\VitiCore\Behavior\Impl\Timestamp;
use CpCreation\VitiCore\Behavior\Impl\Translatable;
use CpCreation\VitiCore\Behavior\Impl\Video;
use CpCreation\VitiCore\News\Repository\NewsRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity(repositoryClass: NewsRepository::class)]
#[ORM\Table(name: 'news_news')]
#[ORM\HasLifecycleCallbacks]
class News implements HasTimestamp, HasDisable, HasVideo, HasFile, HasTranslatable
{
    use Disable;
    use File;
    use Timestamp;
    use Translatable;
    use Video;

    #[ORM\Id]
    #[ORM\Column(type: 'uuid')]
    #[ORM\GeneratedValue(strategy: 'NONE')]
    private UuidInterface $id;

    #[ORM\Column(type: 'datetime', nullable: true)]
    private ?\DateTime $unPublishedAt = null;

    #[ORM\Column(type: 'datetime', nullable: true)]
    private ?\DateTime $publishedAt = null;

    #[ORM\Column(type: 'datetime', nullable: true)]
    private ?\DateTime $eventAt = null;

    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $address = null;

    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $postal = null;

    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $city = null;

    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $country = null;

    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $author = null;

    /**
     * @var Collection<int, NewsImage>
     */
    #[ORM\OneToMany(targetEntity: NewsImage::class, cascade: ['persist', 'remove'], mappedBy: 'news', orphanRemoval: true)]
    #[ORM\OrderBy(['position' => 'ASC'])]
    #[Assert\Valid]
    private Collection $images;

    #[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'news')]
    #[ORM\JoinColumn(name: 'news_category_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
    private ?Category $category = null;

    public function __construct()
    {
        $this->id = Uuid::uuid4();
        $this->disabled = 0;
        $this->images = new ArrayCollection();
    }

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

    public function getUnPublishedAt(): ?\DateTime
    {
        return $this->unPublishedAt;
    }

    public function setUnPublishedAt(?\DateTime $unPublishedAt): void
    {
        $this->unPublishedAt = $unPublishedAt;
    }

    public function getPublishedAt(): ?\DateTime
    {
        return $this->publishedAt;
    }

    public function setPublishedAt(?\DateTime $publishedAt): void
    {
        $this->publishedAt = $publishedAt;
    }

    public function getEventAt(): ?\DateTime
    {
        return $this->eventAt;
    }

    public function setEventAt(?\DateTime $eventAt): void
    {
        $this->eventAt = $eventAt;
    }

    public function getAddress(): ?string
    {
        return $this->address;
    }

    public function setAddress(?string $address): void
    {
        $this->address = $address;
    }

    public function getPostal(): ?string
    {
        return $this->postal;
    }

    public function setPostal(?string $postal): void
    {
        $this->postal = $postal;
    }

    public function getCity(): ?string
    {
        return $this->city;
    }

    public function setCity(?string $city): void
    {
        $this->city = $city;
    }

    public function getAuthor(): ?string
    {
        return $this->author;
    }

    public function setAuthor(?string $author): void
    {
        $this->author = $author;
    }

    public function getCountry(): ?string
    {
        return $this->country;
    }

    public function setCountry(?string $country): void
    {
        $this->country = $country;
    }

    public function addImage(NewsImage $image): self
    {
        if (!$this->images->contains($image)) {
            $this->images[] = $image;
            $image->setNews($this);
        }

        return $this;
    }

    public function removeImage(NewsImage $image): self
    {
        if ($this->images->contains($image)) {
            $this->images->removeElement($image);

            if ($image->getNews() === $this) {
                $image->setNews(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, NewsImage>
     */
    public function getImages(): Collection
    {
        return $this->images;
    }

    public function getCategory(): ?Category
    {
        return $this->category;
    }

    public function setCategory(?Category $category): void
    {
        $this->category = $category;
    }

    #[ORM\PrePersist]
    #[ORM\PreUpdate]
    public function setEventAtValue(): void
    {
        if (!$this->eventAt) {
            if (!$this->publishedAt) {
                $this->eventAt = new \DateTime();
            } else {
                $this->eventAt = $this->publishedAt;
            }
        }
    }
}
