using MangaReader.Core.Search;
using NSubstitute;
using Shouldly;

namespace MangaReader.Tests.Search;

public class MangaSearchCoordinatorTests
{
    [Fact]
    public async Task Search_Manga_From_All_Sources()
    {
        IMangaSearchProvider searchProvider1 = Substitute.For<IMangaSearchProvider>();
        searchProvider1.SourceId.Returns("Manga Source 1");
        searchProvider1.SearchAsync(Arg.Any<string>(), CancellationToken.None)
            .Returns(
            [
                new()
                {
                    Title = "Test Manga 1",
                    Url = "https://mangasource1.com/manga/1",
                    Thumbnail = "https://mangasource1.com/manga/cover/1.png"
                },
                new()
                {
                    Title = "Test Manga 2",
                    Url = "https://mangasource2.com/manga/2",
                    Thumbnail = "https://mangasource2.com/manga/cover/2.png"
                }
            ]);

        IMangaSearchProvider searchProvider2 = Substitute.For<IMangaSearchProvider>();
        searchProvider2.SourceId.Returns("Manga Source 2");
        searchProvider2.SearchAsync(Arg.Any<string>(), CancellationToken.None)
             .Returns(
            [
                new()
                {
                    Title = "Test Manga 3",
                    Url = "https://mangasource3.com/manga/3",
                    Thumbnail = "https://mangasource3.com/manga/cover/3.png"
                },
            ]);

        IMangaSearchProvider searchProvider3 = Substitute.For<IMangaSearchProvider>();
        searchProvider3.SourceId.Returns("Manga Source 3");
        searchProvider3.SearchAsync(Arg.Any<string>(), CancellationToken.None)
            .Returns([]);

        MangaSearchCoordinator searchCoordinator = new([searchProvider1, searchProvider2, searchProvider3]);

        Dictionary<string, MangaSearchResult[]> results = await searchCoordinator.SearchAsync("Test", CancellationToken.None);
        results.Count.ShouldBe(3);

        results.Keys.ToArray().ShouldBe(["Manga Source 1", "Manga Source 2", "Manga Source 3"]);

        results["Manga Source 1"].ShouldBe(
        [
             new()
             {
                 Title = "Test Manga 1",
                 Url = "https://mangasource1.com/manga/1",
                 Thumbnail = "https://mangasource1.com/manga/cover/1.png"
             },
            new()
            {
                Title = "Test Manga 2",
                Url = "https://mangasource2.com/manga/2",
                Thumbnail = "https://mangasource2.com/manga/cover/2.png"
            }
        ]);

        results["Manga Source 2"].ShouldBe(
        [
            new()
            {
                Title = "Test Manga 3",
                Url = "https://mangasource3.com/manga/3",
                Thumbnail = "https://mangasource3.com/manga/cover/3.png"
            }
        ]);

        results["Manga Source 3"].ShouldBe([]);
    }
}