using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Indotalent.Features.Utilities.BookingResource.Cqrs;

public class LookupBookingGroupResponse
{
    public List<LookupBookingGroupItem> BookingGroups { get; set; } = new();
}

public class LookupBookingGroupItem
{
    public string? Id { get; set; }
    public string? Name { get; set; }
}

public record LookupBookingGroupQuery() : IRequest<LookupBookingGroupResponse>;

public class LookupBookingGroupHandler : IRequestHandler<LookupBookingGroupQuery, LookupBookingGroupResponse>
{
    private readonly AppDbContext _context;

    public LookupBookingGroupHandler(AppDbContext context) => _context = context;

    public async Task<LookupBookingGroupResponse> Handle(LookupBookingGroupQuery request, CancellationToken cancellationToken)
    {
        var items = await _context.BookingGroup
            .AsNoTracking()
            .OrderBy(x => x.Name)
            .Select(x => new LookupBookingGroupItem
            {
                Id = x.Id,
                Name = x.Name
            })
            .ToListAsync(cancellationToken);

        return new LookupBookingGroupResponse { BookingGroups = items };
    }
}