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

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

internal sealed class AdminListStepTypesQueryHandler(IUnitOfWork unitOfWork, ICacheService cache)
    : IRequestHandler<AdminListStepTypesQuery, OperationResult<IReadOnlyList<VerificationStepTypeDto>>>
{
    public async ValueTask<OperationResult<IReadOnlyList<VerificationStepTypeDto>>> Handle(
        AdminListStepTypesQuery request, CancellationToken cancellationToken)
    {
        var version = await VerificationCache.VersionAsync(cache, cancellationToken);

        var result = await cache.GetOrCreateAsync(
            VerificationCache.StepTypesKey(version, request.IncludeInactive),
            async ct => await unitOfWork.VerificationRepository.ListStepTypesAsync(request.IncludeInactive, ct),
            VerificationCache.Ttl,
            cancellationToken);

        return OperationResult<IReadOnlyList<VerificationStepTypeDto>>.SuccessResult(result);
    }
}
