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

namespace Indotalent.Features.Pipeline.SalesRepresentative.Cqrs;

public class LookupSalesRepresentativeResponse
{
    public List<LookupItem> SalesTeams { get; set; } = new();
}

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

public record LookupSalesRepresentativeQuery() : IRequest<LookupSalesRepresentativeResponse>;

public class LookupSalesRepresentativeHandler : IRequestHandler<LookupSalesRepresentativeQuery, LookupSalesRepresentativeResponse>
{
    private readonly AppDbContext _context;

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

    public async Task<LookupSalesRepresentativeResponse> Handle(LookupSalesRepresentativeQuery request, CancellationToken cancellationToken)
    {
        var result = new LookupSalesRepresentativeResponse();

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

        return result;
    }
}