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 Update_Upcoming_With_No_Expected_Date_Tests(MariaDbContainerFixture container) : JapaneseIngestionTestsBase(container)
{
    [Fact]
    public async Task Update_Upcoming_With_No_Expected_Date()
    {
        VoiceWorkIngest ingest = new()
        {
            MakerId = "RG00001",
            MakerName = "Some Maker",
            ProductId = "RJ1000001",
            Title = "Some Upcoming Work",
            Description = "Something is coming.",
            Tags = [],
            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, 21),
            RegistrationDate = null
        };

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

        await UpsertAndVerify(dbContext, TokyoLocalToUtc(2025, 01, 05, 10, 0, 0), ingest, new DateTime(2025, 1, 21));

        VoiceWorkIngest updatedIngest = ingest with
        {
            ExpectedDate = null
        };

        // Should be exactly the same
        await UpsertAndVerify(dbContext, TokyoLocalToUtc(2025, 01, 05, 10, 0, 0), updatedIngest, new DateTime(2025, 1, 21));
    }

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

        VoiceWork? voiceWork = await dbContext.VoiceWorks.SingleAsync(v => v.ProductId == ingest.ProductId, TestContext.Current.CancellationToken);
        voiceWork.ShouldNotBeNull();
        voiceWork.ExpectedDate.ShouldBe(expectedDate);
    }
}