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

namespace Indotalent.Features.Pipeline.LeadActivity.Cqrs;

public class LeadActivityLookupResponse
{
    public List<LookupItem> Leads { get; set; } = new();
    public List<LookupItem> ActivityTypes { get; set; } = new();
}

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

public record GetLeadActivityLookupQuery() : IRequest<LeadActivityLookupResponse>;

public class GetLeadActivityLookupHandler : IRequestHandler<GetLeadActivityLookupQuery, LeadActivityLookupResponse>
{
    private readonly AppDbContext _context;

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

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

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

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

        return response;
    }
}