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

        VoiceWorkIngest ingest = new()
        {
            MakerId = "RG00004",
            MakerName = "Never Again",
            ProductId = "RJ3000002",
            Title = "Probably Something with AI",
            Description = "Some description.",
            Tags = [],
            Creators = [],
            WishlistCount = 100,
            Downloads = 0,
            HasTrial = false,
            HasChobit = false,
            AgeRating = AgeRating.R18,
            HasImage = false,
            SupportedLanguages = [SupportedLanguage.Japanese],
            SalesDate = null,
            ExpectedDate = new DateOnly(2025, 2, 1)
        };

        VoiceWorkUpsertResult[] results = await UpsertAsync(dbContext, TokyoLocalToUtc(2025, 01, 09, 16, 00, 00), [ingest]);

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

        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($"Circle {ingest.MakerName} ({ingest.MakerId}) is a spam circle");
    }
}