package com.airlock.iam.flow.shared.infrastructure.exception;

import com.airlock.iam.base.infrastructure.error.ErrorResponse;
import com.airlock.iam.base.misc.log.LogUtil;
import com.airlock.iam.common.infrastructure.exception.IamRestErrorCode;
import com.airlock.iam.elog.api.ELogger;
import com.airlock.iam.elog.api.ELoggerFactory;
import com.airlock.iam.flow.domain.model.flow.ExclusiveRightToOperateOnUserException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;

@Provider
public final class ExclusiveRightToOperateOnUserExceptionMapper implements ExceptionMapper<ExclusiveRightToOperateOnUserException> {
   private static final ELogger LOG = ELoggerFactory.getLogger(ExclusiveRightToOperateOnUserExceptionMapper.class);

   public Response toResponse(ExclusiveRightToOperateOnUserException exception) {
      LOG.info(
         String.format("Cannot start flow because right to operate on user %s exclusively could not be acquired.", LogUtil.logSafeQuote(exception.getUserId()))
      );
      return ErrorResponse.forbidden().errorCode(IamRestErrorCode.UNEXPECTED_CALL).build().toResponse();
   }
}
