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

namespace Indotalent.Features.Utilities.BookingGroup.Cqrs;

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

public record GetBookingGroupByIdQuery(string Id) : IRequest<GetBookingGroupByIdResponse?>;

public class GetBookingGroupByIdHandler : IRequestHandler<GetBookingGroupByIdQuery, GetBookingGroupByIdResponse?>
{
    private readonly AppDbContext _context;

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

    public async Task<GetBookingGroupByIdResponse?> Handle(GetBookingGroupByIdQuery request, CancellationToken cancellationToken)
    {
        return await _context.BookingGroup
            .AsNoTracking()
            .Where(x => x.Id == request.Id)
            .Select(x => new GetBookingGroupByIdResponse
            {
                Id = x.Id,
                Name = x.Name,
                Description = x.Description,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy
            })
            .FirstOrDefaultAsync(cancellationToken);
    }
}