package com.airlock.iam.authentication.domain.model.emailotp;

import com.airlock.iam.authentication.api.domain.model.step.AuthenticationStepResult;
import com.airlock.iam.authentication.api.domain.model.step.AuthenticationStepResultFactory;
import com.airlock.iam.authentication.application.configuration.emailotp.LegacyEmailOtpAuthenticationStepConfig;
import com.airlock.iam.base.api.domain.model.plugin.LogicForConfig;
import com.airlock.iam.common.api.domain.model.authmethod.AuthenticationMethod;
import com.airlock.iam.common.api.domain.model.otp.UnverifiedOtp;
import com.airlock.iam.common.domain.model.masking.StringMasking;
import com.airlock.iam.core.domain.model.authentication.factor.KnownAuthenticationFactorDetail;
import com.airlock.iam.core.misc.impl.authen.EmailOtpAuthenticator;
import com.airlock.iam.flow.api.domain.model.step.StepResult;
import com.airlock.iam.flow.api.domain.model.step.StepSession;
import com.airlock.iam.flow.shared.domain.model.otp.GenericOtpCheckingStep;
import com.airlock.iam.flow.shared.domain.model.otp.OtpCheckStepSession;
import com.airlock.iam.flow.shared.domain.model.otp.OtpCheckingAdapter;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.inject.Inject;
import jakarta.inject.Provider;
import java.util.Optional;

@LogicForConfig(LegacyEmailOtpAuthenticationStepConfig.class)
public class LegacyEmailOtpAuthenticationStepImpl implements GenericOtpCheckingStep {
   private final Provider<OtpCheckingAdapter> otpAuthentication;
   private final Provider<AuthenticationMethod> authMethod;
   private final Provider<Optional<StringMasking>> masking;

   @Inject
   public LegacyEmailOtpAuthenticationStepImpl(
      Provider<OtpCheckingAdapter> otpAuthentication, Provider<AuthenticationMethod> authMethod, Provider<Optional<StringMasking>> masking
   ) {
      this.otpAuthentication = otpAuthentication;
      this.authMethod = authMethod;
      this.masking = masking;
   }

   @Override
   public Optional<StepSession> createSession() {
      return ((OtpCheckingAdapter)this.otpAuthentication.get()).createSession();
   }

   @Override
   public AuthenticationStepResult initialize() {
      AuthenticationStepResult result = ((OtpCheckingAdapter)this.otpAuthentication.get()).initialize(this.stepResultFactory());
      this.addAdditionalAttributes(result);
      return result;
   }

   public AuthenticationStepResult checkOtp(UnverifiedOtp otp) {
      AuthenticationStepResult result = ((OtpCheckingAdapter)this.otpAuthentication.get()).checkOtp(otp, this.stepResultFactory());
      if (result.hasType(StepResult.StepResultType.FAIL_RETRY)) {
         this.addAdditionalAttributes(result);
      }

      return result;
   }

   private void addAdditionalAttributes(AuthenticationStepResult result) {
      String address = this.emailAddressFromSession();
      result.additionalAttributes().put("emailAddress", ((Optional)this.masking.get()).<String>map(m -> m.mask(address)).orElse(address));
   }

   private String emailAddressFromSession() {
      return ((EmailOtpAuthenticator.EmailOtpAuthenticatorSession)((OtpCheckStepSession)((OtpCheckingAdapter)this.otpAuthentication.get())
               .getStepSession()
               .get())
            .getAuthenticationSession())
         .getEmailAddressAsString();
   }

   @Override
   public AuthenticationMethod getAuthenticationMethod() {
      return (AuthenticationMethod)this.authMethod.get();
   }

   @Override
   public String getDescription() {
      return "Legacy Email OTP Authentication Step";
   }

   public AuthenticationStepResultFactory stepResultFactory() {
      return AuthenticationStepResultFactory.forFactorDetail(KnownAuthenticationFactorDetail.OTP_EMAIL);
   }

   @SuppressFBWarnings(justification = "generated code")
   public static LegacyEmailOtpAuthenticationStepImpl.LegacyEmailOtpAuthenticationStepImplBuilder builder() {
      return new LegacyEmailOtpAuthenticationStepImpl.LegacyEmailOtpAuthenticationStepImplBuilder();
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class LegacyEmailOtpAuthenticationStepImplBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private Provider<OtpCheckingAdapter> otpAuthentication;
      @SuppressFBWarnings(justification = "generated code")
      private Provider<AuthenticationMethod> authMethod;
      @SuppressFBWarnings(justification = "generated code")
      private Provider<Optional<StringMasking>> masking;

      @SuppressFBWarnings(justification = "generated code")
      LegacyEmailOtpAuthenticationStepImplBuilder() {
      }

      @SuppressFBWarnings(justification = "generated code")
      public LegacyEmailOtpAuthenticationStepImpl.LegacyEmailOtpAuthenticationStepImplBuilder otpAuthentication(Provider<OtpCheckingAdapter> otpAuthentication) {
         this.otpAuthentication = otpAuthentication;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LegacyEmailOtpAuthenticationStepImpl.LegacyEmailOtpAuthenticationStepImplBuilder authMethod(Provider<AuthenticationMethod> authMethod) {
         this.authMethod = authMethod;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LegacyEmailOtpAuthenticationStepImpl.LegacyEmailOtpAuthenticationStepImplBuilder masking(Provider<Optional<StringMasking>> masking) {
         this.masking = masking;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LegacyEmailOtpAuthenticationStepImpl build() {
         return new LegacyEmailOtpAuthenticationStepImpl(this.otpAuthentication, this.authMethod, this.masking);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "LegacyEmailOtpAuthenticationStepImpl.LegacyEmailOtpAuthenticationStepImplBuilder(otpAuthentication="
            + this.otpAuthentication
            + ", authMethod="
            + this.authMethod
            + ", masking="
            + this.masking
            + ")";
      }
   }
}
