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

namespace Indotalent.Features.Utilities.BookingResource.Cqrs;

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

public record GetBookingResourceByIdQuery(string Id) : IRequest<GetBookingResourceByIdResponse?>;

public class GetBookingResourceByIdHandler : IRequestHandler<GetBookingResourceByIdQuery, GetBookingResourceByIdResponse?>
{
    private readonly AppDbContext _context;

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

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