#nullable enable
using Baya.Application.Contracts.Persistence;
using Baya.Application.Models.Common;
using Baya.Application.Models.Payouts;
using Mediator;

namespace Baya.Application.Features.Payouts.Queries.ListPayoutBatches;

internal sealed class ListPayoutBatchesQueryHandler(IUnitOfWork unitOfWork)
    : IRequestHandler<ListPayoutBatchesQuery, OperationResult<PagedResult<PayoutBatchDto>>>
{
    public async ValueTask<OperationResult<PagedResult<PayoutBatchDto>>> Handle(
        ListPayoutBatchesQuery request, CancellationToken cancellationToken)
    {
        var page = request.Page < 1 ? 1 : request.Page;
        var pageSize = request.PageSize is < 1 or > 100 ? 20 : request.PageSize;

        var result = await unitOfWork.PayoutRepository.ListBatchesAsync(request.Status, page, pageSize, cancellationToken);
        return OperationResult<PagedResult<PayoutBatchDto>>.SuccessResult(result);
    }
}
