using ErsatzTV.Core.Domain;
using ErsatzTV.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;

namespace ErsatzTV.Application.Images;

public class ImageFolderExistsHandler(IDbContextFactory<TvContext> dbContextFactory)
    : IRequestHandler<ImageFolderExists, bool>
{
    public async Task<bool> Handle(ImageFolderExists request, CancellationToken cancellationToken)
    {
        await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);

        return await dbContext.LibraryFolders
            .AsNoTracking()
            .AnyAsync(
                lf => lf.Id == request.LibraryFolderId
                      && lf.LibraryPath.Library.MediaKind == LibraryMediaKind.Images,
                cancellationToken);
    }
}
