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

namespace Indotalent.Features.Thirdparty.VendorGroup.Cqrs;

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

public record GetVendorGroupListQuery() : IRequest<List<GetVendorGroupListResponse>>;

public class GetVendorGroupListHandler : IRequestHandler<GetVendorGroupListQuery, List<GetVendorGroupListResponse>>
{
    private readonly AppDbContext _context;

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

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