using ErsatzTV.Core.Api.Watermarks;
using ErsatzTV.Core.Domain;
using ErsatzTV.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using static ErsatzTV.Application.Watermarks.Mapper;

namespace ErsatzTV.Application.Watermarks;

public class GetAllWatermarksForApiHandler(IDbContextFactory<TvContext> dbContextFactory)
    : IRequestHandler<GetAllWatermarksForApi, List<WatermarkResponseModel>>
{
    public async Task<List<WatermarkResponseModel>> Handle(
        GetAllWatermarksForApi request,
        CancellationToken cancellationToken)
    {
        await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
        List<ChannelWatermark> watermarks = await dbContext.ChannelWatermarks
            .AsNoTracking()
            .ToListAsync(cancellationToken);
        return watermarks.Map(ProjectToResponseModel).ToList();
    }
}
