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

namespace Indotalent.Features.Pipeline.Budget.Cqrs;

public class BudgetLookupResponse
{
    public List<LookupItem> Campaigns { get; set; } = new();
    public List<LookupItem> Statuses { get; set; } = new();
}

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

public record GetBudgetLookupQuery() : IRequest<BudgetLookupResponse>;

public class GetBudgetLookupHandler : IRequestHandler<GetBudgetLookupQuery, BudgetLookupResponse>
{
    private readonly AppDbContext _context;

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

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

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

        response.Statuses = Enum.GetValues(typeof(BudgetStatus))
            .Cast<BudgetStatus>()
            .Select(x => new LookupItem
            {
                Value = (int)x,
                Name = x.ToString()
            })
            .ToList();

        return response;
    }
}