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

        VoiceWorkIngest ingest = new()
        {
            MakerId = "RG00001",
            MakerName = "Good Dreams",
            ProductId = "RJ9999999",
            Title = "EN Title",
            Description = "EN Desc"
        };

        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.Error);
        issue.Message.ShouldBe($"Unable to find voice work for product id: {ingest.ProductId}");
    }
}