<?php

namespace App\Entity;

use App\Repository\MangaRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

#[ORM\Entity(repositoryClass: MangaRepository::class)]
class Manga
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $title = null;

    #[ORM\OneToMany(mappedBy: 'manga', targetEntity: Chapter::class, fetch: 'EAGER', orphanRemoval: true)]
    private Collection $chapters;

    #[ORM\Column(length: 255, unique: true)]
    private ?string $slug = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $imageUrl = null;

    #[ORM\Column(nullable: true)]
    private ?int $publicationYear = null;

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

    #[ORM\Column(type: Types::ARRAY, nullable: true)]
    private ?array $genres = null;

    #[ORM\Column]
    private ?\DateTimeImmutable $createdAt = null;

    #[ORM\Column(nullable: true)]
    private ?float $rating = null;

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

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $externalId = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $status = null;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $thumbnailUrl = null;

    #[ORM\Column]
    private ?bool $monitored = null;

    #[ORM\Column(nullable: true)]
    private ?\DateTimeImmutable $lastMonitoringCheck = null;

    #[ORM\Column(type: Types::JSON, nullable: true)]
    private ?array $AlternativeSlugs = null;

    #[ORM\ManyToMany(targetEntity: ContentSource::class)]
    private Collection $preferredSources;

    public function __construct()
    {
        $this->chapters = new ArrayCollection();
        $this->createdAt = new \DateTimeImmutable();
        $this->preferredSources = 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<int, Chapter>
     */
    public function getChapters(): Collection
    {
        return $this->chapters;
    }

    public function getChapterByNumber(float $number): ?Chapter
    {
        foreach ($this->chapters as $chapter) {
            if ($chapter->getNumber() === $number) {
                return $chapter;
            }
        }
        return null;
    }

    public function addChapter(Chapter $chapter): self
    {
        if (!$this->chapters->contains($chapter)) {
            $this->chapters->add($chapter);
            $chapter->setManga($this);
        }

        return $this;
    }

    public function removeChapter(Chapter $chapter): self
    {
        if ($this->chapters->removeElement($chapter)) {
            // set the owning side to null (unless already changed)
            if ($chapter->getManga() === $this) {
                $chapter->setManga(null);
            }
        }

        return $this;
    }

    public function getSlug(): ?string
    {
        return $this->slug;
    }

    public function setSlug(string $slug): self
    {
        $this->slug = $slug;

        return $this;
    }

    public function getImageUrl(): ?string
    {
        return $this->imageUrl;
    }

    public function setImageUrl(?string $imageUrl): static
    {
        $this->imageUrl = $imageUrl;

        return $this;
    }

    public function getPublicationYear(): ?int
    {
        return $this->publicationYear;
    }

    public function setPublicationYear(?int $publicationYear): static
    {
        $this->publicationYear = $publicationYear;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(?string $description): static
    {
        $this->description = $description;

        return $this;
    }

    public function getGenres(): ?array
    {
        return $this->genres;
    }

    public function setGenres(?array $genres): static
    {
        $this->genres = $genres;

        return $this;
    }

    public function getCreatedAt(): ?\DateTimeImmutable
    {
        return $this->createdAt;
    }

    public function setCreatedAt(\DateTimeImmutable $createdAt): static
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    public function getRating(): ?float
    {
        return $this->rating;
    }

    public function setRating(?float $rating): static
    {
        $this->rating = $rating;

        return $this;
    }

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

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

        return $this;
    }

    public function getExternalId(): ?string
    {
        return $this->externalId;
    }

    public function setExternalId(?string $externalId): static
    {
        $this->externalId = $externalId;

        return $this;
    }

    public function getStatus(): ?string
    {
        return $this->status;
    }

    public function setStatus(?string $status): static
    {
        $this->status = $status;

        return $this;
    }

    public function getThumbnailUrl(): ?string
    {
        return $this->thumbnailUrl;
    }

    public function setThumbnailUrl(?string $thumbnailUrl): static
    {
        $this->thumbnailUrl = $thumbnailUrl;

        return $this;
    }

    public function isMonitored(): ?bool
    {
        return $this->monitored;
    }

    public function setMonitored(bool $monitored): static
    {
        $this->monitored = $monitored;

        return $this;
    }

    public function getAlternativeSlugs(): ?array
    {
        return $this->AlternativeSlugs;
    }

    public function setAlternativeSlugs(?array $AlternativeSlugs): static
    {
        $this->AlternativeSlugs = $AlternativeSlugs;

        return $this;
    }

    /**
     * @return Collection<int, ContentSource>
     */
    public function getPreferredSources(): Collection
    {
        return $this->preferredSources;
    }

    public function addPreferredSource(ContentSource $preferredSource): static
    {
        if (!$this->preferredSources->contains($preferredSource)) {
            $this->preferredSources->add($preferredSource);
        }

        return $this;
    }

    public function removePreferredSource(ContentSource $preferredSource): static
    {
        $this->preferredSources->removeElement($preferredSource);

        return $this;
    }

    public function setPreferredSources(array $sources): self
    {
        $this->preferredSources->clear();
        foreach ($sources as $source) {
            $this->addPreferredSource($source);
        }

        return $this;
    }

    public function getLastMonitoringCheck(): ?\DateTimeImmutable
    {
        return $this->lastMonitoringCheck;
    }

    public function setLastMonitoringCheck(?\DateTimeImmutable $lastMonitoringCheck): self
    {
        $this->lastMonitoringCheck = $lastMonitoringCheck;

        return $this;
    }
}
