using JSMR.Application.VoiceWorks.Commands.Delete;
using JSMR.Infrastructure.Data;
using JSMR.Infrastructure.Data.Repositories.VoiceWorks;
using JSMR.Tests.Fixtures;
using Shouldly;

namespace JSMR.Tests.Data.Repositories.VoiceWorks;

public class Delete_Voice_Work_Tests(MariaDbContainerFixture container) : VoiceWorkRepositoryTests(container)
{
    [Fact]
    public async Task Try_Delete_Invalid_Voice_Work()
    {
        await using AppDbContext dbContext = await GetAppDbContextAsync();
        VoiceWorkWriter writer = new(dbContext);

        int voiceWorkId = 3;
        DeleteVoiceWorkRequest request = new([voiceWorkId]);

        dbContext.VoiceWorks.FirstOrDefault(x => x.VoiceWorkId == voiceWorkId).ShouldNotBeNull();
        dbContext.VoiceWorkSearches.FirstOrDefault(x => x.VoiceWorkId == voiceWorkId).ShouldNotBeNull();

        DeleteVoiceWorkResponse response = await writer.DeleteAsync(request, TestContext.Current.CancellationToken);
        response.Results.Count.ShouldBe(1);
        response.Results.ShouldContainKey(voiceWorkId);
        response.Results[voiceWorkId].ShouldBe(DeleteVoiceWorkStatus.Deleted);

        dbContext.VoiceWorks.FirstOrDefault(x => x.VoiceWorkId == voiceWorkId).ShouldBeNull();
        dbContext.VoiceWorkSearches.FirstOrDefault(x => x.VoiceWorkId == voiceWorkId).ShouldBeNull();
    }

    [Fact]
    public async Task Try_Delete_Valid_Voice_Work()
    {
        await using AppDbContext dbContext = await GetAppDbContextAsync();
        VoiceWorkWriter writer = new(dbContext);

        int voiceWorkId = 1;
        DeleteVoiceWorkRequest request = new([voiceWorkId]);

        dbContext.VoiceWorks.FirstOrDefault(x => x.VoiceWorkId == voiceWorkId).ShouldNotBeNull();
        dbContext.VoiceWorkSearches.FirstOrDefault(x => x.VoiceWorkId == voiceWorkId).ShouldNotBeNull();

        DeleteVoiceWorkResponse response = await writer.DeleteAsync(request, TestContext.Current.CancellationToken);
        response.Results.Count.ShouldBe(1);
        response.Results.ShouldContainKey(voiceWorkId);
        response.Results[voiceWorkId].ShouldBe(DeleteVoiceWorkStatus.NotAllowed);

        dbContext.VoiceWorks.FirstOrDefault(x => x.VoiceWorkId == voiceWorkId).ShouldNotBeNull();
        dbContext.VoiceWorkSearches.FirstOrDefault(x => x.VoiceWorkId == voiceWorkId).ShouldNotBeNull();
    }

    [Fact]
    public async Task Try_Delete_Missing_Voice_Work()
    {
        await using AppDbContext dbContext = await GetAppDbContextAsync();
        VoiceWorkWriter writer = new(dbContext);

        int voiceWorkId = 999;
        DeleteVoiceWorkRequest request = new([voiceWorkId]);

        dbContext.VoiceWorks.FirstOrDefault(x => x.VoiceWorkId == voiceWorkId).ShouldBeNull();
        dbContext.VoiceWorkSearches.FirstOrDefault(x => x.VoiceWorkId == voiceWorkId).ShouldBeNull();

        DeleteVoiceWorkResponse response = await writer.DeleteAsync(request, TestContext.Current.CancellationToken);
        response.Results.Count.ShouldBe(1);
        response.Results.ShouldContainKey(voiceWorkId);
        response.Results[voiceWorkId].ShouldBe(DeleteVoiceWorkStatus.NotFound);

        dbContext.VoiceWorks.FirstOrDefault(x => x.VoiceWorkId == voiceWorkId).ShouldBeNull();
        dbContext.VoiceWorkSearches.FirstOrDefault(x => x.VoiceWorkId == voiceWorkId).ShouldBeNull();
    }
}