<?php

namespace App\Entity;

use App\Repository\ContentSourceRepository;
use App\Service\ChapterUrlGenerator;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: ContentSourceRepository::class)]
class ContentSource
{
    public function __construct()
    {
    }

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

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

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

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

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

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

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

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

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

    #[ORM\Column(length: 20, options: ['default' => 'unknown'])]
    private string $healthStatus = 'unknown';

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

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

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

    public function getBaseUrl(): ?string
    {
        return $this->baseUrl;
    }

    public function setBaseUrl(string $baseUrl): static
    {
        $this->baseUrl = $baseUrl;

        return $this;
    }

    public function getImageSelector(): ?string
    {
        return $this->imageSelector;
    }

    public function setImageSelector(string $imageSelector): static
    {
        $this->imageSelector = $imageSelector;

        return $this;
    }

    public function getNextPageSelector(): ?string
    {
        return $this->NextPageSelector;
    }

    public function setNextPageSelector(?string $NextPageSelector): static
    {
        $this->NextPageSelector = $NextPageSelector;

        return $this;
    }

    public function getChapterUrlFormat(): ?string
    {
        return $this->chapterUrlFormat;
    }

    public function setChapterUrlFormat(string $chapterUrlFormat): static
    {
        $this->chapterUrlFormat = $chapterUrlFormat;

        return $this;
    }

    public function getChapterUrl(string $mangaTitle, float $chapterNumber): string
    {
        $urlGenerator = new ChapterUrlGenerator($this->chapterUrlFormat);
        return $urlGenerator->getChapterUrl($mangaTitle, $chapterNumber);
    }

    public function getScrapingType(): ?string
    {
        return $this->scrapingType;
    }

    public function setScrapingType(string $scrapingType): static
    {
        $this->scrapingType = $scrapingType;

        return $this;
    }

    public function getChapterSelector(): ?string
    {
        return $this->ChapterSelector;
    }

    public function setChapterSelector(?string $ChapterSelector): static
    {
        $this->ChapterSelector = $ChapterSelector;

        return $this;
    }

    public function getTestSlug(): ?string
    {
        return $this->testSlug;
    }

    public function setTestSlug(?string $testSlug): static
    {
        $this->testSlug = $testSlug;

        return $this;
    }

    public function getTestChapterNumber(): ?float
    {
        return $this->testChapterNumber;
    }

    public function setTestChapterNumber(?float $testChapterNumber): static
    {
        $this->testChapterNumber = $testChapterNumber;

        return $this;
    }

    public function getHealthStatus(): string
    {
        return $this->healthStatus;
    }

    public function setHealthStatus(string $healthStatus): static
    {
        $this->healthStatus = $healthStatus;

        return $this;
    }

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

    public function setHealthLastTestedAt(?\DateTimeImmutable $healthLastTestedAt): static
    {
        $this->healthLastTestedAt = $healthLastTestedAt;

        return $this;
    }

    public function getHealthLastError(): ?string
    {
        return $this->healthLastError;
    }

    public function setHealthLastError(?string $healthLastError): static
    {
        $this->healthLastError = $healthLastError;

        return $this;
    }

    public function getCleanBaseUrl(): string
    {
        return preg_replace(
            '/^(https?:\/\/)?(www\.)?|\/+$/',
            '',
            $this->baseUrl
        );
    }
}
