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

namespace Baya.Application.Features.Refunds.Queries.ListRefunds;

internal sealed class ListRefundsQueryHandler(IUnitOfWork unitOfWork)
    : IRequestHandler<ListRefundsQuery, OperationResult<PagedResult<RefundListItemDto>>>
{
    public async ValueTask<OperationResult<PagedResult<RefundListItemDto>>> Handle(ListRefundsQuery 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.RefundRepository.ListAsync(request.BookingId, request.Status, page, pageSize, cancellationToken);
        return OperationResult<PagedResult<RefundListItemDto>>.SuccessResult(result);
    }
}
