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

namespace Indotalent.Features.Pipeline.Campaign.Cqrs;

public class CampaignLookupResponse
{
    public List<LookupItem> SalesTeams { get; set; } = new();
    public List<LookupItem> Statuses { get; set; } = new();
    public List<LookupItem> BudgetStatuses { get; set; } = new();
    public List<LookupItem> ExpenseStatuses { get; set; } = new();
}

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

public record GetCampaignLookupQuery() : IRequest<CampaignLookupResponse>;

public class GetCampaignLookupHandler : IRequestHandler<GetCampaignLookupQuery, CampaignLookupResponse>
{
    private readonly AppDbContext _context;
    public GetCampaignLookupHandler(AppDbContext context) => _context = context;

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

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

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

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

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

        return response;
    }
}