package com.airlock.iam.flow.shared.application.configuration.captcha;

import com.airlock.iam.flow.shared.api.application.configuration.captcha.CaptchaProviderConfig;
import com.airlock.iam.plugin.api.annotation.config.Groups;
import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.property.GroupProperty;
import com.airlock.iam.plugin.api.annotation.property.StringProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.annotation.Nullable;

@Plugin(
   displayName = "reCAPTCHA",
   description = "Requires a user to solve a reCAPTCHA challenge.</br>\nIf you use an HTTPS truststore, make sure to add the root certificate for https://google.com.</br>\nFurther, you need to ensure that the correct Content Security Policy (CSP) is set. reCAPTCHA requires\n<ul>\n<li><tt>script-src https://www.google.com/recaptcha/ https://www.gstatic.com/recaptcha/</tt></li>\n<li><tt>frame-src https://www.google.com/recaptcha/ https://recaptcha.google.com/recaptcha/</tt></li>\n</ul>\nConsult the official <a href=\"https://www.google.com/recaptcha/about/\" target=\"_blank\">reCAPTCHA\ndocumentation</a> for further information.</br>\nNote: Currently only reCAPTCHA v2 (\"I'm not a robot\" Checkbox) is supported."
)
@Groups(groupSequence = {"Basic Settings", "Proxy Settings"}, openGroups = "Basic Settings")
public final class ReCaptchaConfig implements CaptchaProviderConfig {
   static final String PROXY_SETTINGS = "Proxy Settings";
   @StringProperty(
      description = "<p>The site key can be assumed to be public knowledge and identifies the associated reCAPTCHA account.</p><p>The site key can be found on the reCAPTCHA <a href=\"https://www.google.com/recaptcha/admin\" target=\"_blank\"> admin page</a>.</p>",
      optional = false
   )
   @GroupProperty(groupIdentifier = "Basic Settings")
   private String siteKey;
   @StringProperty(
      description = "<p>The secret is used to validate the CAPTCHA challenge response on the reCAPTCHA server. While a leaked secret key doesn't impact the security or validity of this CAPTCHA method, its misuse can infer costs as it is used for quota calculations at the CAPTCHA provider (similar to an API key).</p><p>The secret key can be found on the reCaptcha <a href=\"https://www.google.com/recaptcha/admin\" target=\"_blank\" > admin page</a>.</p>",
      isSensitive = true,
      optional = false
   )
   @GroupProperty(groupIdentifier = "Basic Settings")
   private String secretKey;
   @StringProperty(
      description = "URI of a HTTP proxy the connector should use. If the port component of the URI is absent then a default port of 8080 is assumed. If this property is left empty then no proxy will be used.",
      displayName = "Proxy URI",
      optional = true,
      exampleValues = "https://proxy.company.com"
   )
   @GroupProperty(groupIdentifier = "Proxy Settings")
   private String proxyUri;
   private static final String FIELD_PROXY_URI = "proxyUri";
   @StringProperty(description = "Username for the proxy if proxy authentication is used.", optional = true)
   @GroupProperty(groupIdentifier = "Proxy Settings")
   private String proxyLoginUser;
   @StringProperty(description = "Password for the proxy if proxy authentication is used.", optional = true, isSensitive = true)
   @GroupProperty(groupIdentifier = "Proxy Settings")
   private String proxyLoginPassword;

   public String getSiteKey() {
      return this.siteKey;
   }

   public String getSecretKey() {
      return this.secretKey;
   }

   @Nullable
   public String getProxyUri() {
      return this.proxyUri;
   }

   @Nullable
   public String getProxyLoginUser() {
      return this.proxyLoginUser;
   }

   @Nullable
   public String getProxyLoginPassword() {
      return this.proxyLoginPassword;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public ReCaptchaConfig(String siteKey, String secretKey, String proxyUri, String proxyLoginUser, String proxyLoginPassword) {
      this.siteKey = siteKey;
      this.secretKey = secretKey;
      this.proxyUri = proxyUri;
      this.proxyLoginUser = proxyLoginUser;
      this.proxyLoginPassword = proxyLoginPassword;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class ReCaptchaConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private String siteKey;
      @SuppressFBWarnings(justification = "generated code")
      private String secretKey;
      @SuppressFBWarnings(justification = "generated code")
      private String proxyUri;
      @SuppressFBWarnings(justification = "generated code")
      private String proxyLoginUser;
      @SuppressFBWarnings(justification = "generated code")
      private String proxyLoginPassword;

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

      @SuppressFBWarnings(justification = "generated code")
      public ReCaptchaConfig.ReCaptchaConfigBuilder siteKey(String siteKey) {
         this.siteKey = siteKey;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ReCaptchaConfig.ReCaptchaConfigBuilder secretKey(String secretKey) {
         this.secretKey = secretKey;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ReCaptchaConfig.ReCaptchaConfigBuilder proxyUri(String proxyUri) {
         this.proxyUri = proxyUri;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ReCaptchaConfig.ReCaptchaConfigBuilder proxyLoginUser(String proxyLoginUser) {
         this.proxyLoginUser = proxyLoginUser;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ReCaptchaConfig.ReCaptchaConfigBuilder proxyLoginPassword(String proxyLoginPassword) {
         this.proxyLoginPassword = proxyLoginPassword;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ReCaptchaConfig build() {
         return new ReCaptchaConfig(this.siteKey, this.secretKey, this.proxyUri, this.proxyLoginUser, this.proxyLoginPassword);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "ReCaptchaConfig.ReCaptchaConfigBuilder(siteKey="
            + this.siteKey
            + ", secretKey="
            + this.secretKey
            + ", proxyUri="
            + this.proxyUri
            + ", proxyLoginUser="
            + this.proxyLoginUser
            + ", proxyLoginPassword="
            + this.proxyLoginPassword
            + ")";
      }
   }
}
