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

namespace Indotalent.Features.Utilities.BookingResource.Cqrs;

public class GetBookingResourceListResponse
{
    public string? Id { get; set; }
    public string? Name { get; set; }
    public string? BookingGroupName { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
}

public record GetBookingResourceListQuery() : IRequest<List<GetBookingResourceListResponse>>;

public class GetBookingResourceListHandler : IRequestHandler<GetBookingResourceListQuery, List<GetBookingResourceListResponse>>
{
    private readonly AppDbContext _context;

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

    public async Task<List<GetBookingResourceListResponse>> Handle(GetBookingResourceListQuery request, CancellationToken cancellationToken)
    {
        return await _context.BookingResource
            .AsNoTracking()
            .Include(x => x.BookingGroup)
            .OrderBy(x => x.Name)
            .Select(x => new GetBookingResourceListResponse
            {
                Id = x.Id,
                Name = x.Name,
                BookingGroupName = x.BookingGroup != null ? x.BookingGroup.Name : string.Empty,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy
            })
            .ToListAsync(cancellationToken);
    }
}