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

namespace Indotalent.Features.Pipeline.Expense.Cqrs;

public class ExpenseLookupResponse
{
    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 GetExpenseLookupQuery() : IRequest<ExpenseLookupResponse>;

public class GetExpenseLookupHandler : IRequestHandler<GetExpenseLookupQuery, ExpenseLookupResponse>
{
    private readonly AppDbContext _context;

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

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

        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(ExpenseStatus))
            .Cast<ExpenseStatus>()
            .Select(x => new LookupItem
            {
                Value = (int)x,
                Name = x.ToString()
            })
            .ToList();

        return response;
    }
}