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 Fail_Attempted_Update_With_Decreased_Downloads_Tests(MariaDbContainerFixture container) : JapaneseIngestionTestsBase(container)
{
    [Fact]
    public async Task Fail_Attempted_Update_With_Decreased_Downloads()
    {
        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, 00, 00, 00), [ingest]);

        VoiceWork? voiceWork = await dbContext.VoiceWorks.FirstOrDefaultAsync(v => v.ProductId == ingest.ProductId, TestContext.Current.CancellationToken);
        voiceWork.ShouldNotBeNull();
        voiceWork.Downloads.ShouldBe(10);

        results.Length.ShouldBe(1);
        results.Count(r => r.Status == VoiceWorkUpsertStatus.Inserted).ShouldBe(1);
        results.Count(r => r.Status == VoiceWorkUpsertStatus.Updated).ShouldBe(0);
        results.Count(r => r.Status == VoiceWorkUpsertStatus.Skipped).ShouldBe(0);
        results.Sum(r => r.Issues.Count).ShouldBe(0);

        VoiceWorkIngest updatedIngest = ingest with
        {
            Downloads = 9
        };

        VoiceWorkUpsertResult[] updatedResults = await UpsertAsync(dbContext, TokyoLocalToUtc(2025, 01, 16, 00, 00, 00), [updatedIngest]);

        voiceWork = await dbContext.VoiceWorks.FirstOrDefaultAsync(v => v.ProductId == ingest.ProductId, TestContext.Current.CancellationToken);
        voiceWork.ShouldNotBeNull();
        voiceWork.Downloads.ShouldBe(10);

        updatedResults.Length.ShouldBe(1);
        updatedResults.Count(r => r.Status == VoiceWorkUpsertStatus.Inserted).ShouldBe(0);
        updatedResults.Count(r => r.Status == VoiceWorkUpsertStatus.Updated).ShouldBe(0);
        updatedResults.Count(r => r.Status == VoiceWorkUpsertStatus.Skipped).ShouldBe(1);
        updatedResults.Sum(r => r.Issues.Count).ShouldBe(1);

        VoiceWorkUpsertIssue issue = updatedResults[0].Issues.ElementAt(0);
        issue.Severity.ShouldBe(VoiceWorkUpsertIssueSeverity.Error);
        issue.Message.ShouldBe($"Downloads have decreased from {voiceWork.Downloads} to {updatedIngest.Downloads}");
    }
}