using JSMR.Application.VoiceWorks.Commands.SetFavorite;
using JSMR.Domain.Entities;
using JSMR.Infrastructure.Data;
using JSMR.Infrastructure.Data.Repositories.VoiceWorks;
using JSMR.Tests.Fixtures;
using Microsoft.EntityFrameworkCore;
using Shouldly;

namespace JSMR.Tests.Data.Repositories.VoiceWorks;

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

        await SetFavoriteAndVerifyAsync(dbContext, writer, 1, true);
        await SetFavoriteAndVerifyAsync(dbContext, writer, 1, true);
        await SetFavoriteAndVerifyAsync(dbContext, writer, 1, false);
        await SetFavoriteAndVerifyAsync(dbContext, writer, 1, false);
        await SetFavoriteAndVerifyAsync(dbContext, writer, 1, true);
        await SetFavoriteAndVerifyAsync(dbContext, writer, 1, false);
    }

    private static async Task SetFavoriteAndVerifyAsync(AppDbContext dbContext, VoiceWorkWriter writer, int voiceWorkId, bool isFavorite)
    {
        SetVoiceWorkFavoriteRequest request = new(
            VoiceWorkId: voiceWorkId,
            IsFavorite: isFavorite
        );

        SetVoiceWorkFavoriteResponse response = await writer.SetFavoriteAsync(request, TestContext.Current.CancellationToken);

        response.VoiceWorkId.ShouldBe(voiceWorkId);
        response.IsFavorite.ShouldBe(isFavorite);

        VoiceWork? voiceWork = await dbContext.VoiceWorks
            .SingleAsync(v => v.VoiceWorkId == voiceWorkId, TestContext.Current.CancellationToken);

        voiceWork.ShouldNotBeNull();
        voiceWork.Favorite.ShouldBe(isFavorite);
    }
}