package com.airlock.iam.publicselfservice.infrastructure.factory.shared;

import com.airlock.iam.base.api.infrastructure.plugin.ModuleForConfig;
import com.airlock.iam.common.domain.model.token.secretquestions.SecretQuestionsRepository;
import com.airlock.iam.flow.api.domain.model.session.FlowSession;
import com.airlock.iam.flow.infrastructure.step.StepSessionModuleHelper;
import com.airlock.iam.publicselfservice.application.configuration.steps.SecretQuestionsIdentityVerificationStepConfig;
import com.airlock.iam.publicselfservice.domain.model.flow.session.SecretQuestionsIdentityVerificationStepSession;
import com.airlock.iam.publicselfservice.domain.model.step.SecretQuestionsIdentityVerificationStepSettings;
import com.airlock.iam.publicselfservice.domain.service.SecretQuestionSimulationService;
import com.google.inject.Exposed;
import com.google.inject.PrivateModule;
import com.google.inject.Provides;

@ModuleForConfig(SecretQuestionsIdentityVerificationStepConfig.class)
public final class SecretQuestionsIdentityVerificationStepModule extends PrivateModule {
   @Exposed
   @Provides
   SecretQuestionsIdentityVerificationStepSettings provideSecretQuestionsIdentityVerificationStepSettings(SecretQuestionsIdentityVerificationStepConfig config) {
      return SecretQuestionsIdentityVerificationStepSettings.builder()
         .checkUsingLatin1Encoding(config.getSecretQuestionsSettings().isCheckUsingLatin1Encoding())
         .questionResourceKeys(config.getSecretQuestionsSettings().getQuestionResourceKeys())
         .numberOfRequiredQuestions(config.getSecretQuestionsSettings().getRequiredNumberOfProvisionedAnswers())
         .maxFailedAttempts(config.getMaxFailedAttempts())
         .authMethodId(config.getAuthenticationMethodId())
         .passwordHash(config.getSecretQuestionsSettings().getHashFunctionPlugin())
         .build();
   }

   @Exposed
   @Provides
   SecretQuestionSimulationService providePublicSelfServiceSecretQuestionSimulationService(SecretQuestionsIdentityVerificationStepSettings settings) {
      return new SecretQuestionSimulationService(settings);
   }

   @Exposed
   @Provides
   SecretQuestionsRepository provideSecretQuestionsRepository(SecretQuestionsIdentityVerificationStepConfig config) {
      return new SecretQuestionsRepository(config.getSecretQuestionsSettings());
   }

   @Exposed
   @Provides
   SecretQuestionsIdentityVerificationStepSession provideSecretQuestionsIdentityVerificationStepSession(FlowSession flowSession) {
      return StepSessionModuleHelper.retrieveStepSession(flowSession, SecretQuestionsIdentityVerificationStepSession.class);
   }

   protected void configure() {
   }
}
