#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.GetBatchDetail;

internal sealed class GetBatchDetailQueryHandler(IUnitOfWork unitOfWork)
    : IRequestHandler<GetBatchDetailQuery, OperationResult<PayoutBatchDetailDto>>
{
    public async ValueTask<OperationResult<PayoutBatchDetailDto>> Handle(
        GetBatchDetailQuery request, CancellationToken cancellationToken)
    {
        var page = request.Page < 1 ? 1 : request.Page;
        var pageSize = request.PageSize is < 1 or > 200 ? 50 : request.PageSize;

        var detail = await unitOfWork.PayoutRepository.GetBatchDetailAsync(request.BatchId, page, pageSize, cancellationToken);
        return detail is null
            ? OperationResult<PayoutBatchDetailDto>.NotFoundResult("Payout batch not found.")
            : OperationResult<PayoutBatchDetailDto>.SuccessResult(detail);
    }
}
