package com.airlock.iam.authentication.infrastructure.factory;

import com.airlock.iam.authentication.application.configuration.targetapp.TargetApplicationsConfig;
import com.airlock.iam.authentication.application.configuration.ui.AuthenticationUiConfig;
import com.airlock.iam.authentication.infrastructure.service.ui.AuthenticationUiService;
import com.airlock.iam.base.api.domain.model.exception.ServiceConfigurationException;
import com.google.inject.Exposed;
import com.google.inject.PrivateModule;
import com.google.inject.Provides;
import com.google.inject.servlet.RequestScoped;
import jakarta.inject.Provider;
import java.util.Optional;

public final class AuthenticationUiServiceModule extends PrivateModule {
   @Provides
   TargetApplicationsConfig provideAuthenticationFlowsConfig(Optional<TargetApplicationsConfig> authenticationFlowsConfig) {
      return ServiceConfigurationException.checkPresent(authenticationFlowsConfig, "An authentication flows configuration must be present");
   }

   @Provides
   @Exposed
   Optional<AuthenticationUiService> provideOptionalAuthenticationUiService(
      Optional<AuthenticationUiConfig> authenticationUiConfig, Provider<AuthenticationUiService> uiService
   ) {
      return authenticationUiConfig.map(config -> (AuthenticationUiService)uiService.get());
   }

   protected void configure() {
      this.bind(AuthenticationUiService.class).in(RequestScoped.class);
      this.expose(AuthenticationUiService.class);
   }
}
