using JSMR.Application.Scanning.Contracts;
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_And_Scan_Again_Later_Tests(MariaDbContainerFixture container) : JapaneseIngestionTestsBase(container)
{
    [Fact]
    public async Task Insert_New_Release_And_Scan_Again_Later()
    {
        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
        };

        await UpsertAndVerify(dbContext, TokyoLocalToUtc(2025, 01, 15, 00, 00, 00), ingest, VoiceWorkStatus.NewRelease);
        await UpsertAndVerify(dbContext, TokyoLocalToUtc(2025, 01, 15, 15, 59, 59), ingest, VoiceWorkStatus.NewRelease);
        await UpsertAndVerify(dbContext, TokyoLocalToUtc(2025, 01, 15, 16, 00, 00), ingest, VoiceWorkStatus.Available);
        await UpsertAndVerify(dbContext, TokyoLocalToUtc(2025, 01, 16, 00, 00, 00), ingest, VoiceWorkStatus.Available);
    }

    private static async Task UpsertAndVerify(AppDbContext dbContext, DateTime dateTime, VoiceWorkIngest ingest, VoiceWorkStatus status)
    {
        await UpsertAsync(dbContext, dateTime, [ingest]);

        VoiceWork? voiceWork = await dbContext.VoiceWorks.SingleAsync(v => v.ProductId == ingest.ProductId, TestContext.Current.CancellationToken);
        voiceWork.Status.ShouldBe((byte)status);
    }
}