<?php

namespace App\Tests\Domain\Manga\Adapter;

use App\Domain\Manga\Domain\Contract\Provider\MangaProviderInterface;
use App\Domain\Manga\Domain\Model\Manga;
use App\Domain\Manga\Domain\Model\MangaCollection;
use App\Domain\Manga\Domain\Model\ValueObject\ExternalId;

class InMemoryMangaProvider implements MangaProviderInterface
{
    /** @var Manga[] */
    private array $mangas;

    /**
     * @param Manga[] $mangas
     */
    public function __construct(array $mangas = [])
    {
        $this->mangas = $mangas;
    }

    public function search(string $title): MangaCollection
    {
        $results = array_filter(
            $this->mangas,
            fn (Manga $manga) => str_contains(
                strtolower($manga->getTitle()->getValue()),
                strtolower($title)
            )
        );

        return new MangaCollection($results);
    }

    public function findByExternalId(ExternalId $externalId): ?Manga
    {
        foreach ($this->mangas as $manga) {
            if ($manga->getExternalId() && $manga->getExternalId()->getValue() === $externalId->getValue()) {
                return $manga;
            }
        }

        return null;
    }

    public function discover(array $sourceExternalIds): MangaCollection
    {
        return new MangaCollection([]);
    }
} 