using MangaReader.Core.Common;
using MangaReader.Core.Data;
using MangaReader.Core.Metadata;
using MangaReader.Core.Pipeline;
using MangaReader.Tests.Utilities;
using Shouldly;

namespace MangaReader.Tests.Pipeline;

public class MangaPipelineTests(TestDbContextFactory factory) : IClassFixture<TestDbContextFactory>
{
    [Fact]
    public async Task RunAsync_SavesMangaTitlesChaptersGenres()
    {
        using MangaContext context = factory.CreateContext();
        var pipeline = new MangaPipeline(context);

        var sourceManga = new SourceManga
        {
            Title = new()
            {
                Name = "Fullmetal Alchemist",
                Language = Language.English
            },
            AlternateTitles =
            [
                new()
                {
                    Name = "Hagane no Renkinjutsushi",
                    Language = Language.Romaji
                }
            ],
            Genres = ["Action", "Adventure"],
            Contributors =
            [
                new()
                {
                    Name = "Manga Author",
                    Role = ContributorRole.Author
                },
                new()
                {
                    Name = "Manga Author",
                    Role = ContributorRole.Artist
                },
                new()
                {
                    Name = "Helper Artist",
                    Role = ContributorRole.Artist
                }
            ],
            Chapters =
            [
                new()
                {
                    Number = 1,
                    Title = "The Two Alchemists",
                    Volume = 1,
                    Url = string.Empty
                }
            ]
        };

        MangaMetadataPipelineRequest request = new()
        {
            SourceName = "MySource",
            SourceUrl = "https://wwww.mymangasource.org/my-manga",
            SourceManga = sourceManga
        };

        await pipeline.RunMetadataAsync(request, CancellationToken.None);

        context.Mangas.ShouldHaveSingleItem();
        context.MangaTitles.Count().ShouldBe(2);
        context.MangaTitles.Where(mt => mt.IsPrimary).ShouldHaveSingleItem();
        context.MangaTitles.Where(mt => mt.IsPrimary).First().Name.ShouldBe("Fullmetal Alchemist");
        context.MangaTitles.Where(mt => mt.IsPrimary).First().Language.ShouldBe(Language.English);
        context.Genres.Count().ShouldBe(2);

        context.MangaContributors.Count().ShouldBe(3);
        context.MangaContributors.ElementAt(0).Contributor.Name.ShouldBe("Manga Author");
        context.MangaContributors.ElementAt(0).Role.ShouldBe(ContributorRole.Author);
        context.MangaContributors.ElementAt(1).Contributor.Name.ShouldBe("Manga Author");
        context.MangaContributors.ElementAt(1).Role.ShouldBe(ContributorRole.Artist);
        context.MangaContributors.ElementAt(2).Contributor.Name.ShouldBe("Helper Artist");
        context.MangaContributors.ElementAt(2).Role.ShouldBe(ContributorRole.Artist);

        context.SourceChapters.ShouldHaveSingleItem();
    }
}