<?php

namespace App\Tests\Domain\Scraping\Adapter;

use App\Domain\Scraping\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Domain\Scraping\Domain\Model\Manga;

class InMemoryMangaRepository implements MangaRepositoryInterface
{
    private array $mangas = [];

    public function __construct()
    {
        // Ajoute un manga par défaut pour les tests
        $this->mangas['test-manga'] = new Manga(
            'test-manga',
            'Test Manga',
            'test-manga',
            'A test manga description',
            'Test Author',
            '2024',
            false, // monitored
            [], // preferredSources
            [] // alternativeSlugs
        );

        // Ajoute un manga avec des sources préférées pour les tests
        $this->mangas['test-manga-with-sources'] = new Manga(
            'test-manga-with-sources',
            'Test Manga With Sources',
            'test-manga-with-sources',
            'A test manga with preferred sources',
            'Test Author',
            '2024',
            false, // monitored
            ['test-source'], // preferredSources
            [] // alternativeSlugs
        );
    }

    public function getById(string $id): ?Manga
    {
        return $this->mangas[$id] ?? null;
    }

    public function save(Manga $manga): void
    {
        $this->mangas[$manga->getId()] = $manga;
    }

    public function updatePreferredSources(string $mangaId, array $sourceIds): void
    {
        if (isset($this->mangas[$mangaId])) {
            $manga = $this->mangas[$mangaId];
            $updatedManga = new Manga(
                $manga->getId(),
                $manga->getTitle(),
                $manga->getSlug(),
                $manga->getDescription(),
                $manga->getAuthor(),
                $manga->getPublicationYear(),
                $manga->isMonitored(), // monitored
                $sourceIds, // preferredSources
                $manga->getAlternativeSlugs() // alternativeSlugs
            );
            $this->mangas[$mangaId] = $updatedManga;
        }
    }

    public function clear(): void
    {
        $this->mangas = [];
    }
}
