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

import com.airlock.iam.common.domain.model.user.PrincipalInfo;
import com.airlock.iam.common.domain.model.user.PrincipalInfoImpl;
import com.airlock.iam.flow.domain.model.session.FlowSessionManager;
import com.airlock.iam.flow.shared.domain.model.user.TentativeFlowUserId;
import com.airlock.iam.flow.shared.infrastructure.factor.PrincipalInfoFactory;
import com.google.inject.Exposed;
import com.google.inject.PrivateModule;
import com.google.inject.Provides;

public final class PublicSelfServicePrincipalInfoModule extends PrivateModule {
   @Exposed
   @Provides
   PrincipalInfo providePrincipalInfo(FlowSessionManager flowManager, TentativeFlowUserId tentativeUserId) {
      return PrincipalInfoFactory.createInfoForTentativeId(flowManager)
         .or(() -> tentativeUserId.getUserIdentity().map(PrincipalInfoImpl::forTentativeIdentity))
         .orElseGet(PrincipalInfoImpl::forUnknownIdentity);
   }

   protected void configure() {
   }
}
