package com.airlock.iam.publicselfservice.application.configuration.steps;

import com.airlock.iam.common.api.application.configuration.cronto.CrontoMessageProviderConfig;
import com.airlock.iam.core.domain.model.cronto.CrontoCheckingSettings;
import com.airlock.iam.core.misc.cronto.CrontoHandler;
import com.airlock.iam.flow.api.application.configuration.dynamicactivation.DynamicStepActivationConfig;
import com.airlock.iam.flow.api.application.configuration.selection.FlowConditionConfig;
import com.airlock.iam.flow.api.application.configuration.step.InteractiveGotoTargetConfig;
import com.airlock.iam.flow.api.application.configuration.step.StepIdConfig;
import com.airlock.iam.flow.api.application.configuration.tag.TagConfig;
import com.airlock.iam.flow.api.application.configuration.valueprovider.ValueMapProviderConfig;
import com.airlock.iam.flow.shared.application.configuration.cronto.CommonCrontoCheckingStepConfig;
import com.airlock.iam.flow.shared.domain.model.cronto.CrontoCheckingStep;
import com.airlock.iam.flow.types.api.application.configuration.step.StepConfigForPublicSelfServiceFlows;
import com.airlock.iam.plugin.api.annotation.config.Groups;
import com.airlock.iam.plugin.api.annotation.config.NextActions;
import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.license.PluginLicenseRestriction;
import com.airlock.iam.plugin.api.annotation.property.BooleanProperty;
import com.airlock.iam.plugin.api.annotation.property.GroupProperty;
import com.airlock.iam.plugin.api.annotation.property.PluginProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.annotation.Nullable;
import java.util.List;
import java.util.Map;

@Plugin(
   description = "<p>Cronto approval step for public self-service flows.</p><p>Note that unlike identity verification steps, approval steps require an existing user and cannot prevent username enumeration (no stealth mode). It is therefore important that approval steps are only used <em>after</em> an identity verification step.</p>",
   displayName = "Cronto Public Self-Service Approval Step"
)
@PluginLicenseRestriction(moduleAffiliation = "Cronto")
@Groups(openGroups = "Basic Settings", groupSequence = {"Basic Settings", "Tags/Guards", "Flow Control", "Advanced Settings"})
@NextActions(CrontoCheckingStep.CrontoAuthStepActions.class)
public class CrontoPublicSelfServiceApprovalStepConfig extends CommonCrontoCheckingStepConfig implements StepConfigForPublicSelfServiceFlows {
   @PluginProperty(description = "Configures how to create Cronto messages.", optional = false)
   @GroupProperty(groupIdentifier = "Basic Settings")
   private CrontoMessageProviderConfig messageProvider;
   @BooleanProperty(
      description = "<p>If this flag is set and there is no push-enabled device for the user, public self-service is not possible.</p><p>This feature may be used for mobile application services, where showing a cryptogram on the same device is not useful.</p>",
      defaultValue = "false",
      optional = true
   )
   @GroupProperty(groupIdentifier = "Advanced Settings")
   private boolean allowPushDevicesOnly;
   @BooleanProperty(
      description = "If enabled, the step also asks for push device selection if there is only one push device enabled. Since the selection always includes the \"offline\" option, this can be used for \"app-to-app\" setups, where push messages should never be sent.",
      defaultValue = "false",
      optional = true
   )
   @GroupProperty(groupIdentifier = "Advanced Settings")
   private boolean pushSelectionForSingleDevice;

   public CrontoCheckingSettings getSettings() {
      return CrontoCheckingSettings.builder()
         .authenticatePushDevicesOnly(this.allowPushDevicesOnly)
         .pushSelectionForSingleDevice(this.pushSelectionForSingleDevice)
         .showLoginIdOnPush(false)
         .maxResponseRetries(this.getMaxResponseRetries())
         .crontoHandler(this.getCrontoHandler())
         .challengeType(CrontoHandler.CrontoChallengeType.TRANSACTION_APPROVAL)
         .build();
   }

   protected CrontoPublicSelfServiceApprovalStepConfig(
      FlowConditionConfig skipCondition,
      FlowConditionConfig preCondition,
      boolean requiresActivation,
      List<TagConfig> tagsOnSuccess,
      @Nullable StepIdConfig stepId,
      List<InteractiveGotoTargetConfig> interactiveGotoTargets,
      List<DynamicStepActivationConfig> dynamicStepActivations,
      Map<String, StepIdConfig> onFailureGotos,
      List<ValueMapProviderConfig> customResponseAttributes,
      List<ValueMapProviderConfig> customFailureResponseAttributes,
      CrontoHandler crontoHandler,
      CrontoMessageProviderConfig messageProvider,
      int maxResponseRetries,
      boolean allowPushDevicesOnly,
      boolean pushSelectionForSingleDevice,
      String authenticationMethodId
   ) {
      super(
         skipCondition,
         preCondition,
         requiresActivation,
         tagsOnSuccess,
         stepId,
         interactiveGotoTargets,
         dynamicStepActivations,
         onFailureGotos,
         customResponseAttributes,
         customFailureResponseAttributes,
         crontoHandler,
         maxResponseRetries,
         authenticationMethodId
      );
      this.messageProvider = messageProvider;
      this.allowPushDevicesOnly = allowPushDevicesOnly;
      this.pushSelectionForSingleDevice = pushSelectionForSingleDevice;
   }

   public CrontoMessageProviderConfig getMessageProvider() {
      return this.messageProvider;
   }

   public boolean isAllowPushDevicesOnly() {
      return this.allowPushDevicesOnly;
   }

   public boolean isPushSelectionForSingleDevice() {
      return this.pushSelectionForSingleDevice;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public CrontoPublicSelfServiceApprovalStepConfig() {
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class CrontoPublicSelfServiceApprovalStepConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private FlowConditionConfig skipCondition;
      @SuppressFBWarnings(justification = "generated code")
      private FlowConditionConfig preCondition;
      @SuppressFBWarnings(justification = "generated code")
      private boolean requiresActivation;
      @SuppressFBWarnings(justification = "generated code")
      private List<TagConfig> tagsOnSuccess;
      @SuppressFBWarnings(justification = "generated code")
      private StepIdConfig stepId;
      @SuppressFBWarnings(justification = "generated code")
      private List<InteractiveGotoTargetConfig> interactiveGotoTargets;
      @SuppressFBWarnings(justification = "generated code")
      private List<DynamicStepActivationConfig> dynamicStepActivations;
      @SuppressFBWarnings(justification = "generated code")
      private Map<String, StepIdConfig> onFailureGotos;
      @SuppressFBWarnings(justification = "generated code")
      private List<ValueMapProviderConfig> customResponseAttributes;
      @SuppressFBWarnings(justification = "generated code")
      private List<ValueMapProviderConfig> customFailureResponseAttributes;
      @SuppressFBWarnings(justification = "generated code")
      private CrontoHandler crontoHandler;
      @SuppressFBWarnings(justification = "generated code")
      private CrontoMessageProviderConfig messageProvider;
      @SuppressFBWarnings(justification = "generated code")
      private int maxResponseRetries;
      @SuppressFBWarnings(justification = "generated code")
      private boolean allowPushDevicesOnly;
      @SuppressFBWarnings(justification = "generated code")
      private boolean pushSelectionForSingleDevice;
      @SuppressFBWarnings(justification = "generated code")
      private String authenticationMethodId;

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

      @SuppressFBWarnings(justification = "generated code")
      public CrontoPublicSelfServiceApprovalStepConfig.CrontoPublicSelfServiceApprovalStepConfigBuilder skipCondition(FlowConditionConfig skipCondition) {
         this.skipCondition = skipCondition;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoPublicSelfServiceApprovalStepConfig.CrontoPublicSelfServiceApprovalStepConfigBuilder preCondition(FlowConditionConfig preCondition) {
         this.preCondition = preCondition;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoPublicSelfServiceApprovalStepConfig.CrontoPublicSelfServiceApprovalStepConfigBuilder requiresActivation(boolean requiresActivation) {
         this.requiresActivation = requiresActivation;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoPublicSelfServiceApprovalStepConfig.CrontoPublicSelfServiceApprovalStepConfigBuilder tagsOnSuccess(List<TagConfig> tagsOnSuccess) {
         this.tagsOnSuccess = tagsOnSuccess;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoPublicSelfServiceApprovalStepConfig.CrontoPublicSelfServiceApprovalStepConfigBuilder stepId(@Nullable StepIdConfig stepId) {
         this.stepId = stepId;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoPublicSelfServiceApprovalStepConfig.CrontoPublicSelfServiceApprovalStepConfigBuilder interactiveGotoTargets(
         List<InteractiveGotoTargetConfig> interactiveGotoTargets
      ) {
         this.interactiveGotoTargets = interactiveGotoTargets;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoPublicSelfServiceApprovalStepConfig.CrontoPublicSelfServiceApprovalStepConfigBuilder dynamicStepActivations(
         List<DynamicStepActivationConfig> dynamicStepActivations
      ) {
         this.dynamicStepActivations = dynamicStepActivations;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoPublicSelfServiceApprovalStepConfig.CrontoPublicSelfServiceApprovalStepConfigBuilder onFailureGotos(Map<String, StepIdConfig> onFailureGotos) {
         this.onFailureGotos = onFailureGotos;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoPublicSelfServiceApprovalStepConfig.CrontoPublicSelfServiceApprovalStepConfigBuilder customResponseAttributes(
         List<ValueMapProviderConfig> customResponseAttributes
      ) {
         this.customResponseAttributes = customResponseAttributes;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoPublicSelfServiceApprovalStepConfig.CrontoPublicSelfServiceApprovalStepConfigBuilder customFailureResponseAttributes(
         List<ValueMapProviderConfig> customFailureResponseAttributes
      ) {
         this.customFailureResponseAttributes = customFailureResponseAttributes;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoPublicSelfServiceApprovalStepConfig.CrontoPublicSelfServiceApprovalStepConfigBuilder crontoHandler(CrontoHandler crontoHandler) {
         this.crontoHandler = crontoHandler;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoPublicSelfServiceApprovalStepConfig.CrontoPublicSelfServiceApprovalStepConfigBuilder messageProvider(
         CrontoMessageProviderConfig messageProvider
      ) {
         this.messageProvider = messageProvider;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoPublicSelfServiceApprovalStepConfig.CrontoPublicSelfServiceApprovalStepConfigBuilder maxResponseRetries(int maxResponseRetries) {
         this.maxResponseRetries = maxResponseRetries;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoPublicSelfServiceApprovalStepConfig.CrontoPublicSelfServiceApprovalStepConfigBuilder allowPushDevicesOnly(boolean allowPushDevicesOnly) {
         this.allowPushDevicesOnly = allowPushDevicesOnly;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoPublicSelfServiceApprovalStepConfig.CrontoPublicSelfServiceApprovalStepConfigBuilder pushSelectionForSingleDevice(
         boolean pushSelectionForSingleDevice
      ) {
         this.pushSelectionForSingleDevice = pushSelectionForSingleDevice;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoPublicSelfServiceApprovalStepConfig.CrontoPublicSelfServiceApprovalStepConfigBuilder authenticationMethodId(String authenticationMethodId) {
         this.authenticationMethodId = authenticationMethodId;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoPublicSelfServiceApprovalStepConfig build() {
         return new CrontoPublicSelfServiceApprovalStepConfig(
            this.skipCondition,
            this.preCondition,
            this.requiresActivation,
            this.tagsOnSuccess,
            this.stepId,
            this.interactiveGotoTargets,
            this.dynamicStepActivations,
            this.onFailureGotos,
            this.customResponseAttributes,
            this.customFailureResponseAttributes,
            this.crontoHandler,
            this.messageProvider,
            this.maxResponseRetries,
            this.allowPushDevicesOnly,
            this.pushSelectionForSingleDevice,
            this.authenticationMethodId
         );
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "CrontoPublicSelfServiceApprovalStepConfig.CrontoPublicSelfServiceApprovalStepConfigBuilder(skipCondition="
            + this.skipCondition
            + ", preCondition="
            + this.preCondition
            + ", requiresActivation="
            + this.requiresActivation
            + ", tagsOnSuccess="
            + this.tagsOnSuccess
            + ", stepId="
            + this.stepId
            + ", interactiveGotoTargets="
            + this.interactiveGotoTargets
            + ", dynamicStepActivations="
            + this.dynamicStepActivations
            + ", onFailureGotos="
            + this.onFailureGotos
            + ", customResponseAttributes="
            + this.customResponseAttributes
            + ", customFailureResponseAttributes="
            + this.customFailureResponseAttributes
            + ", crontoHandler="
            + this.crontoHandler
            + ", messageProvider="
            + this.messageProvider
            + ", maxResponseRetries="
            + this.maxResponseRetries
            + ", allowPushDevicesOnly="
            + this.allowPushDevicesOnly
            + ", pushSelectionForSingleDevice="
            + this.pushSelectionForSingleDevice
            + ", authenticationMethodId="
            + this.authenticationMethodId
            + ")";
      }
   }
}
