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

namespace Indotalent.Features.Inventory.TransactionReport.Cqrs;

public class InventoryTransactionReportLookupResponse
{
    public List<LookupItem> Warehouses { get; set; } = new();
}

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

public record GetInventoryTransactionReportLookupQuery() : IRequest<InventoryTransactionReportLookupResponse>;

public class GetInventoryTransactionReportLookupHandler : IRequestHandler<GetInventoryTransactionReportLookupQuery, InventoryTransactionReportLookupResponse>
{
    private readonly AppDbContext _context;
    public GetInventoryTransactionReportLookupHandler(AppDbContext context) => _context = context;

    public async Task<InventoryTransactionReportLookupResponse> Handle(GetInventoryTransactionReportLookupQuery request, CancellationToken cancellationToken)
    {
        var response = new InventoryTransactionReportLookupResponse();

        response.Warehouses = await _context.Warehouse.AsNoTracking()
            .Where(x => x.SystemWarehouse == false)
            .OrderBy(x => x.Name)
            .Select(x => new LookupItem { Id = x.Id, Name = x.Name })
            .ToListAsync(cancellationToken);

        return response;
    }
}