<?php

namespace App\Service;

use Symfony\Component\HttpClient\Exception\InvalidArgumentException;

class ChapterUrlGenerator
{
    private string $chapterUrlFormat;

    public function __construct(string $chapterUrlFormat)
    {
        $this->chapterUrlFormat = $chapterUrlFormat;
        $this->validateUrlFormat($chapterUrlFormat);
    }

    public function getChapterUrl(string $mangaTitle, float $chapterNumber): string
    {
        $placeholders = [
            '{chapterNumber}' => $chapterNumber,
            '{slug}' => $mangaTitle,
        ];

        return str_replace(array_keys($placeholders), array_values($placeholders), $this->chapterUrlFormat);
    }

    private function validateUrlFormat(string $format): void
    {
        if (!str_contains($format, '{slug}')) {
            throw new InvalidArgumentException("The URL format must contain both {slug} and {chapterNumber} placeholders.");
        }
    }

}
