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

namespace Baya.Application.Features.Verification.Commands.DeactivateStepType;

internal sealed class AdminDeactivateStepTypeCommandHandler(IUnitOfWork unitOfWork, ICacheService cache)
    : IRequestHandler<AdminDeactivateStepTypeCommand, OperationResult<bool>>
{
    public async ValueTask<OperationResult<bool>> Handle(AdminDeactivateStepTypeCommand request, CancellationToken cancellationToken)
    {
        var type = await unitOfWork.VerificationRepository.GetStepTypeByIdAsync(request.Id, cancellationToken);
        if (type is null)
            return OperationResult<bool>.NotFoundResult("Step type not found.");

        type.IsActive = false;
        await unitOfWork.CommitAsync();
        await VerificationCache.InvalidateStepTypesAsync(cache, cancellationToken);

        return OperationResult<bool>.SuccessResult(true);
    }
}
