<?php

namespace App\Domain\Setting\Domain\Model;

final class ContentSource
{
    public function __construct(
        private ?int $id,
        private string $baseUrl,
        private string $chapterUrlFormat,
        private string $scrapingType,
        private ?string $imageSelector = null,
        private ?string $nextPageSelector = null,
        private ?string $chapterSelector = null,
        private ?string $testSlug = null,
        private ?float $testChapterNumber = null,
        private string $healthStatus = 'unknown',
        private ?\DateTimeImmutable $healthLastTestedAt = null,
        private ?string $healthLastError = null,
    ) {
    }

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

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

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

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

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

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

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

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

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

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

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

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

    public function updateTestConfig(?string $testSlug, ?float $testChapterNumber): void
    {
        $this->testSlug = $testSlug;
        $this->testChapterNumber = $testChapterNumber;
    }

    public function updateHealthStatus(string $status, ?\DateTimeImmutable $testedAt = null, ?string $error = null): void
    {
        $this->healthStatus = $status;
        $this->healthLastTestedAt = $testedAt;
        $this->healthLastError = $error;
    }

    public function updateId(int $id): void
    {
        $this->id = $id;
    }

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

    public static function create(
        string $baseUrl,
        string $chapterUrlFormat,
        string $scrapingType,
        ?string $imageSelector = null,
        ?string $nextPageSelector = null,
        ?string $chapterSelector = null,
        ?string $testSlug = null,
        ?float $testChapterNumber = null,
    ): self {
        return new self(
            id: null,
            baseUrl: $baseUrl,
            chapterUrlFormat: $chapterUrlFormat,
            scrapingType: $scrapingType,
            imageSelector: $imageSelector,
            nextPageSelector: $nextPageSelector,
            chapterSelector: $chapterSelector,
            testSlug: $testSlug,
            testChapterNumber: $testChapterNumber,
        );
    }

    public function update(
        string $baseUrl,
        string $chapterUrlFormat,
        string $scrapingType,
        ?string $imageSelector = null,
        ?string $nextPageSelector = null,
        ?string $chapterSelector = null,
        ?string $testSlug = null,
        ?float $testChapterNumber = null,
    ): void {
        $this->baseUrl = $baseUrl;
        $this->chapterUrlFormat = $chapterUrlFormat;
        $this->scrapingType = $scrapingType;
        $this->imageSelector = $imageSelector;
        $this->nextPageSelector = $nextPageSelector;
        $this->chapterSelector = $chapterSelector;
        $this->testSlug = $testSlug;
        $this->testChapterNumber = $testChapterNumber;
    }
}
