using ErsatzTV.Application.MediaCollections;
using ErsatzTV.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using static ErsatzTV.Application.MediaCollections.Mapper;

namespace ErsatzTV.Application.Search;

public class SearchMultiCollectionsHandler(IDbContextFactory<TvContext> dbContextFactory)
    : IRequestHandler<SearchMultiCollections, List<MultiCollectionViewModel>>
{
    public async Task<List<MultiCollectionViewModel>> Handle(
        SearchMultiCollections request,
        CancellationToken cancellationToken)
    {
        await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
        return await dbContext.MultiCollections
            .AsNoTracking()
            // Hide system-owned auto-tune weighted artifacts (#425) from the scheduling picker: selecting one
            // into a user schedule would let a later channel delete cascade away that schedule item.
            .Where(mc => mc.OwnedByChannelId == null)
            .Where(mc => EF.Functions.Like(mc.Name, $"%{request.Query}%"))
            .OrderBy(mc => mc.Name)
            .Take(10)
            .ToListAsync(cancellationToken)
            .Map(list => list.Map(ProjectToViewModel).ToList());
    }
}
