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

namespace Indotalent.Features.Pipeline.SalesTeam.Cqrs;

public class GetSalesTeamListResponse
{
    public string? Id { get; set; }
    public string? Name { get; set; }
    public string? Description { get; set; }
}

public record GetSalesTeamListQuery() : IRequest<List<GetSalesTeamListResponse>>;

public class GetSalesTeamListHandler : IRequestHandler<GetSalesTeamListQuery, List<GetSalesTeamListResponse>>
{
    private readonly AppDbContext _context;

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

    public async Task<List<GetSalesTeamListResponse>> Handle(GetSalesTeamListQuery request, CancellationToken cancellationToken)
    {
        return await _context.SalesTeam
            .AsNoTracking()
            .OrderBy(x => x.Name)
            .Select(x => new GetSalesTeamListResponse
            {
                Id = x.Id,
                Name = x.Name,
                Description = x.Description
            })
            .ToListAsync(cancellationToken);
    }
}