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 = "hCAPTCHA",
   description = "Requires a user to solve a hCAPTCHA challenge.</br>\nIf you use an HTTPS truststore, make sure to add the root certificate for https://hcaptcha.com.</br>\nFurther, you need to ensure that the correct Content Security Policy (CSP) is set. hCAPTCHA requires\n<ul>\n<li><tt>script-src https://hcaptcha.com https://*.hcaptcha.com</tt></li>\n<li><tt>frame-src https://hcaptcha.com https://*.hcaptcha.com</tt></li>\n<li><tt>style-src https://hcaptcha.com https://*.hcaptcha.com</tt></li>\n<li><tt>connect-src https://hcaptcha.com https://*.hcaptcha.com</tt></li>\n</ul>\nConsult the official <a href=\"https://docs.hcaptcha.com/\" target=\"_blank\">hCAPTCHA documentation</a> for further\ninformation."
)
@Groups(groupSequence = {"Basic Settings", "Proxy Settings"}, openGroups = "Basic Settings")
public final class HCaptchaConfig 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 hCAPTCHA account.</p><p>The site key can be found on the hCAPTCHA <a href=\"https://dashboard.hcaptcha.com/settings\" target=\"_blank\"> profile page</a>.</p>",
      optional = false
   )
   @GroupProperty(groupIdentifier = "Basic Settings")
   private String siteKey;
   @StringProperty(
      description = "<p>The secret key is used to validate the CAPTCHA challenge response on the hCAPTCHA 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 hCAPTCHA <a href=\"https://dashboard.hcaptcha.com/settings\" target=\"_blank\" > profile 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, exampleValues = "proxyLogin")
   @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 HCaptchaConfig.HCaptchaConfigBuilder builder() {
      return new HCaptchaConfig.HCaptchaConfigBuilder();
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public HCaptchaConfig(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 HCaptchaConfigBuilder {
      @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")
      HCaptchaConfigBuilder() {
      }

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

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

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

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

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

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

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