using JSMR.Application.Scanning.Contracts;
using JSMR.Application.Scanning.Ports;
using JSMR.Infrastructure.Data;
using JSMR.Infrastructure.Ingestion;
using JSMR.Tests.Fixtures;
using Microsoft.EntityFrameworkCore;
using Shouldly;

namespace JSMR.Tests.Ingestion.English;

public class English_Fail_Attempted_Insert_When_Not_English_Tests(MariaDbContainerFixture container) : EnglishIngestionTestBase(container)
{
    [Fact]
    public async Task Fail_Attempted_Insert_When_Not_English()
    {
        await using AppDbContext dbContext = await GetAppDbContextAsync();

        VoiceWorkIngest ingest = new()
        {
            MakerId = "RG00001",
            MakerName = "Good Dreams",
            ProductId = "RJ0000001",
            Title = "すごく快適なASMR",
            Description = "最高の製品です！"
        };

        EnglishVoiceWorkUpdater updater = new(dbContext, LanguageIdentifier);
        VoiceWorkUpsertResult[] results = await updater.UpsertAsync([ingest], CancellationToken.None);

        int englishVoiceWorkCount = await dbContext.EnglishVoiceWorks.CountAsync(CancellationToken.None);
        englishVoiceWorkCount.ShouldBe(0);

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

        VoiceWorkUpsertIssue issue = results[0].Issues.ElementAt(0);
        issue.Severity.ShouldBe(VoiceWorkUpsertIssueSeverity.Information);
        issue.Message.ShouldBe("Product title and/or description is not in English");
    }
}