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

namespace Indotalent.Features.Utilities.ProgramManager.Cqrs;

public class ProgramManagerLookupResponse
{
    public List<LookupItem> Resources { get; set; } = new();
    public List<LookupItem> Statuses { get; set; } = new();
    public List<LookupItem> Priorities { get; set; } = new();
}

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

public record GetProgramManagerLookupQuery() : IRequest<ProgramManagerLookupResponse>;

public class GetProgramManagerLookupHandler : IRequestHandler<GetProgramManagerLookupQuery, ProgramManagerLookupResponse>
{
    private readonly AppDbContext _context;

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

    public async Task<ProgramManagerLookupResponse> Handle(GetProgramManagerLookupQuery request, CancellationToken cancellationToken)
    {
        var response = new ProgramManagerLookupResponse();

        response.Resources = await _context.ProgramManagerResource
            .AsNoTracking()
            .OrderBy(x => x.Name)
            .Select(x => new LookupItem { Id = x.Id, Name = x.Name })
            .ToListAsync(cancellationToken);

        response.Statuses = Enum.GetValues(typeof(ProgramManagerStatus))
            .Cast<ProgramManagerStatus>()
            .Select(e => new LookupItem { Value = (int)e, Name = e.GetDescription() })
            .ToList();

        response.Priorities = Enum.GetValues(typeof(ProgramManagerPriority))
            .Cast<ProgramManagerPriority>()
            .Select(e => new LookupItem { Value = (int)e, Name = e.GetDescription() })
            .ToList();

        return response;
    }
}