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

namespace Indotalent.Features.Thirdparty.VendorCategory.Cqrs;

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

public record GetVendorCategoryListQuery() : IRequest<List<GetVendorCategoryListResponse>>;

public class GetVendorCategoryListHandler : IRequestHandler<GetVendorCategoryListQuery, List<GetVendorCategoryListResponse>>
{
    private readonly AppDbContext _context;

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

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