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

namespace Indotalent.Features.Utilities.BookingManager.Cqrs;

public class GetBookingByIdResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public string? Subject { get; set; }
    public DateTime? StartTime { get; set; }
    public DateTime? EndTime { get; set; }
    public string? StartTimezone { get; set; }
    public string? EndTimezone { get; set; }
    public string? Location { get; set; }
    public string? Description { get; set; }
    public bool? IsAllDay { get; set; }
    public bool? IsReadOnly { get; set; }
    public bool? IsBlock { get; set; }
    public string? RecurrenceRule { get; set; }
    public string? RecurrenceID { get; set; }
    public string? FollowingID { get; set; }
    public string? RecurrenceException { get; set; }
    public BookingStatus Status { get; set; }
    public string? BookingResourceId { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
}

public record GetBookingByIdQuery(string Id) : IRequest<GetBookingByIdResponse?>;

public class GetBookingByIdHandler : IRequestHandler<GetBookingByIdQuery, GetBookingByIdResponse?>
{
    private readonly AppDbContext _context;

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

    public async Task<GetBookingByIdResponse?> Handle(GetBookingByIdQuery request, CancellationToken cancellationToken)
    {
        return await _context.Booking
            .AsNoTracking()
            .Where(x => x.Id == request.Id)
            .Select(x => new GetBookingByIdResponse
            {
                Id = x.Id,
                AutoNumber = x.AutoNumber,
                Subject = x.Subject,
                StartTime = x.StartTime,
                EndTime = x.EndTime,
                StartTimezone = x.StartTimezone,
                EndTimezone = x.EndTimezone,
                Location = x.Location,
                Description = x.Description,
                IsAllDay = x.IsAllDay,
                IsReadOnly = x.IsReadOnly,
                IsBlock = x.IsBlock,
                RecurrenceRule = x.RecurrenceRule,
                RecurrenceID = x.RecurrenceID,
                FollowingID = x.FollowingID,
                RecurrenceException = x.RecurrenceException,
                Status = x.Status,
                BookingResourceId = x.BookingResourceId,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy
            })
            .FirstOrDefaultAsync(cancellationToken);
    }
}