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

namespace Indotalent.Features.Utilities.BookingManager.Cqrs;

public class LookupBookingDataResponse
{
    public List<LookupResourceItem> Resources { get; set; } = new();
    public List<LookupStatusItem> Statuses { get; set; } = new();
}

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

public class LookupStatusItem
{
    public int Value { get; set; }
    public string? Name { get; set; }
}

public record LookupBookingDataQuery() : IRequest<LookupBookingDataResponse>;

public class LookupBookingDataHandler : IRequestHandler<LookupBookingDataQuery, LookupBookingDataResponse>
{
    private readonly AppDbContext _context;

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

    public async Task<LookupBookingDataResponse> Handle(LookupBookingDataQuery request, CancellationToken cancellationToken)
    {
        var resources = await _context.BookingResource
            .AsNoTracking()
            .OrderBy(x => x.Name)
            .Select(x => new LookupResourceItem { Id = x.Id, Name = x.Name })
            .ToListAsync(cancellationToken);

        var statuses = Enum.GetValues(typeof(BookingStatus))
            .Cast<BookingStatus>()
            .Select(x => new LookupStatusItem
            {
                Value = (int)x,
                Name = x.GetDescription()
            })
            .ToList();

        return new LookupBookingDataResponse
        {
            Resources = resources,
            Statuses = statuses
        };
    }
}