using LiteCqrs;
using Microsoft.EntityFrameworkCore;
using TeleWave.Application.Common.Interfaces;

namespace TeleWave.Application.Broadcast.ListChannels;

public sealed class ListChannelsQueryHandler(IAppDbContext dbContext)
    : IQueryHandler<ListChannelsQuery, IReadOnlyList<ChannelSummaryDto>>
{
    public async Task<IReadOnlyList<ChannelSummaryDto>> Handle(
        ListChannelsQuery query,
        CancellationToken cancellationToken
    )
    {
        return await dbContext
            .Channels.AsNoTracking()
            .OrderBy(c => c.Name)
            .Select(c => new ChannelSummaryDto(c.Id, c.Name, c.Slug, c.IsEnabled))
            .ToListAsync(cancellationToken);
    }
}
