using ErsatzTV.Core.Api.Filler;
using ErsatzTV.Infrastructure.Data;
using ErsatzTV.Infrastructure.Extensions;
using Microsoft.EntityFrameworkCore;
using static ErsatzTV.Application.Filler.Mapper;

namespace ErsatzTV.Application.Filler;

public class GetFillerPresetByIdForApiHandler(IDbContextFactory<TvContext> dbContextFactory)
    : IRequestHandler<GetFillerPresetByIdForApi, Option<FillerPresetFullResponseModel>>
{
    public async Task<Option<FillerPresetFullResponseModel>> Handle(
        GetFillerPresetByIdForApi request,
        CancellationToken cancellationToken)
    {
        await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
        return await dbContext.FillerPresets
            .AsNoTracking()
            .SelectOneAsync(fp => fp.Id, fp => fp.Id == request.Id, cancellationToken)
            .MapT(ProjectToFullResponseModel);
    }
}
