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

namespace Indotalent.Features.Pipeline.Lead.Cqrs;

public class LeadLookupResponse
{
    public List<LookupItem> Campaigns { get; set; } = new();
    public List<LookupItem> SalesTeams { get; set; } = new();
    public List<LookupItem> PipelineStages { get; set; } = new();
    public List<LookupItem> ClosingStatuses { get; set; } = new();
}

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

public record GetLeadLookupQuery() : IRequest<LeadLookupResponse>;

public class GetLeadLookupHandler : IRequestHandler<GetLeadLookupQuery, LeadLookupResponse>
{
    private readonly AppDbContext _context;

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

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

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

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

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

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

        return response;
    }
}