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_Release_With_New_Tags_And_Creators_Tests(MariaDbContainerFixture container) : JapaneseIngestionTestsBase(container)
{
    [Fact]
    public async Task Insert_New_Release_With_New_Tags_And_Creators()
    {
        await using AppDbContext dbContext = await GetAppDbContextAsync();

        VoiceWorkIngest ingest = new()
        {
            MakerId = "RG10001",
            MakerName = "New Dreams",
            ProductId = "RJ2000001",
            Title = "Day One Release",
            Description = "Releasing now.",
            Tags = ["アイドル", "メガネ"],
            Creators = ["かの仔"],
            WishlistCount = 50,
            Downloads = 10,
            HasTrial = false,
            HasChobit = false,
            StarRating = null,
            Votes = null,
            AgeRating = AgeRating.AllAges,
            HasImage = true,
            SupportedLanguages = [SupportedLanguage.Japanese],
            SalesDate = new DateOnly(2025, 1, 15),
            ExpectedDate = null
        };

        VoiceWorkUpsertResult[] results = await UpsertAsync(dbContext, TokyoLocalToUtc(2025, 01, 15, 9, 0, 0), [ingest]);

        VoiceWork? voiceWork = await dbContext.VoiceWorks.SingleAsync(v => v.ProductId == "RJ2000001", TestContext.Current.CancellationToken);
        voiceWork.ShouldNotBeNull();
        voiceWork.ProductName.ShouldBe("Day One Release");
        voiceWork.Description.ShouldBe("Releasing now.");
        voiceWork.SalesDate.ShouldNotBeNull();
        voiceWork.ExpectedDate.ShouldBeNull();
        voiceWork.Status.ShouldBe((byte)VoiceWorkStatus.NewRelease);

        voiceWork.Circle.ShouldNotBeNull();
        voiceWork.Circle.MakerId.ShouldBe("RG10001");
        voiceWork.Circle.Name.ShouldBe("New Dreams");

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

        voiceWork.Creators.OrderBy(c => c.Position).Select(vwc => vwc.Creator?.Name).ToArray()
            .ShouldBe(["かの仔"]);
    }
}