using JSMR.Application.Scanning.Contracts;
using JSMR.Application.Scanning.Ports;
using JSMR.Domain.Entities;
using JSMR.Domain.Enums;
using JSMR.Domain.ValueObjects;
using JSMR.Infrastructure.Data;
using JSMR.Tests.Fixtures;
using Microsoft.EntityFrameworkCore;
using Shouldly;

namespace JSMR.Tests.Ingestion.Japanese;

public class Insert_New_Upcoming_With_Existing_Tags_And_Creators_Tests(MariaDbContainerFixture container) : JapaneseIngestionTestsBase(container)
{
    [Fact]
    public async Task Insert_New_Upcoming_With_Existing_Tags_And_Creators()
    {
        VoiceWorkIngest[] insertNewUpcomingIngests =
        [
            new()
            {
                MakerId = "RG00001",
                MakerName = "Good Dreams",
                ProductId = "RJ1000001",
                Title = "Brand New Preview",
                Description = "Teaser only.",
                Tags = ["ASMR", "メイド" ],
                Creators = ["秋野かえで"],
                WishlistCount = 250,
                Downloads = 0,
                HasTrial = false,
                HasChobit = false,
                StarRating = null,
                Votes = null,
                AgeRating = AgeRating.R15,
                HasImage = true,
                SupportedLanguages = [SupportedLanguage.Japanese],
                SalesDate = null,
                ExpectedDate = new DateOnly(2025, 1, 20),
                RegistrationDate = null
            }
        ];

        await using AppDbContext dbContext = await GetAppDbContextAsync();
        DateTime currentDateTime = TokyoLocalToUtc(2025, 01, 05, 10, 0, 0);

        VoiceWorkUpsertResult[] results = await UpsertAsync(dbContext, currentDateTime, insertNewUpcomingIngests);

        VoiceWork? voiceWork = await dbContext.VoiceWorks.SingleAsync(v => v.ProductId == "RJ1000001", TestContext.Current.CancellationToken);
        voiceWork.ShouldNotBeNull();
        voiceWork.ProductName.ShouldBe("Brand New Preview");
        voiceWork.Description.ShouldBe("Teaser only.");
        voiceWork.SalesDate.ShouldBeNull();
        voiceWork.ExpectedDate.ShouldNotBeNull();
        voiceWork.Status.ShouldBe((byte)VoiceWorkStatus.NewAndUpcoming);

        voiceWork.Tags.OrderBy(t => t.Position).Select(vwt => vwt.Tag?.Name).ToArray()
            .ShouldBe(["ASMR", "メイド"]);

        voiceWork.Creators.OrderBy(c => c.Position).Select(vwc => vwc.Creator?.Name).ToArray()
            .ShouldBe(["秋野かえで"]);
    }
}