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

namespace Indotalent.Features.Thirdparty.VendorContact.Cqrs;

public class LookupVendorContactResponse
{
    public List<LookupItem> Vendors { get; set; } = new();
}

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

public record LookupVendorContactQuery() : IRequest<LookupVendorContactResponse>;

public class LookupVendorContactHandler : IRequestHandler<LookupVendorContactQuery, LookupVendorContactResponse>
{
    private readonly AppDbContext _context;

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

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

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

        return result;
    }
}