#nullable enable
using Baya.Application.Common;
using Baya.Application.Contracts.Common;
using Baya.Application.Contracts.Persistence;
using Baya.Application.Models.Common;
using Baya.Application.Models.Verification;
using Baya.Domain.Entities.Verification;
using Mediator;

namespace Baya.Application.Features.Verification.Queries.ListPendingSteps;

internal sealed class AdminListPendingStepsQueryHandler(IUnitOfWork unitOfWork, IObjectStorage objectStorage)
    : IRequestHandler<AdminListPendingStepsQuery, OperationResult<PagedResult<AdminPendingStepDto>>>
{
    public async ValueTask<OperationResult<PagedResult<AdminPendingStepDto>>> Handle(
        AdminListPendingStepsQuery request, CancellationToken cancellationToken)
    {
        var (page, pageSize) = Pagination.Normalize(request.Page, request.PageSize);
        var status = VerificationCodes.TryParseStepStatus(request.Status);

        var result = await unitOfWork.VerificationRepository.ListPendingStepsAsync(
            status, page, pageSize, key => objectStorage.GetUrl(key), cancellationToken);

        return OperationResult<PagedResult<AdminPendingStepDto>>.SuccessResult(result);
    }
}
