package com.airlock.iam.oauth2.application.configuration.scope;

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.RegexProperty;
import com.airlock.iam.plugin.api.annotation.property.StringProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.regex.Pattern;

@Plugin(description = "Defines translation rules for OAuth 2.0 scopes to human-readable explanations.", displayName = "OAuth 2.0 Scope Translation Entry")
@PluginLicenseRestriction(moduleAffiliation = "OAuthServer")
public class OAuth2ScopeTranslationEntry {
   @RegexProperty(
      description = "Regular expression matched against the OAuth 2.0 scopes. If the expression matches, the \"Translation Key\" is used instead of the scope name.",
      optional = false,
      exampleValues = {"Auth.*", "admin"}
   )
   private Pattern scopePattern;
   @StringProperty(
      description = "<p>Specifies the property key for the scope's human-readable name or description. The system looks up translations in the following order:</p>\n\n<p>1. Checks strings_xx.properties file for the specified key</p>\n<p>2. If not found, frontend translations in the xx.properties files of the Loginapp SDK are used, using the following pattern: <code>&lt;prefix&gt;.&lt;scope&gt;</code></p>\n<p>3. If there is no frontend translation, the scope name is used as is</p>\n\n<p>Available frontend translation prefixes by context:</p>\n<table>\n\t<tr>\n\t\t<td>Authentication Flow:</td>\n\t\t<td><code>authentication.oauth2.consent.scope.</code></td>\n\t</tr>\n\t<tr>\n\t\t<td>OAuth 2.0 Consent Management:</td>\n\t\t<td><code>protected.oauth2.consents.consent.scope.</code></td>\n\t</tr>\n\t<tr>\n\t\t<td>OAuth 2.0 Session Management:</td>\n\t\t<td><code>protected.oauth2.sessions.session.scope.</code></td>\n\t</tr>\n</table>\n<p>\nExample frontend translations for the scope <code>read</code> on all three pages:\n<pre><code>\n\tauthentication.oauth2.consent.scope.read=Read user profile data\n\tprotected.oauth2.consents.consent.scope.read=Read user profile data\n\tprotected.oauth2.sessions.session.scope.read=Read user profile data\n</code></pre>\n</p>\n",
      optional = false,
      displayName = "Translation Key"
   )
   private String translationKey;
   private static final String PROPERTY_KEY_DISPLAY_NAME = "Translation Key";

   public Pattern getScopePattern() {
      return this.scopePattern;
   }

   public String getTranslationKey() {
      return this.translationKey;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public OAuth2ScopeTranslationEntry(Pattern scopePattern, String translationKey) {
      this.scopePattern = scopePattern;
      this.translationKey = translationKey;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class OAuth2ScopeTranslationEntryBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private Pattern scopePattern;
      @SuppressFBWarnings(justification = "generated code")
      private String translationKey;

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

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2ScopeTranslationEntry.OAuth2ScopeTranslationEntryBuilder scopePattern(Pattern scopePattern) {
         this.scopePattern = scopePattern;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2ScopeTranslationEntry.OAuth2ScopeTranslationEntryBuilder translationKey(String translationKey) {
         this.translationKey = translationKey;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2ScopeTranslationEntry build() {
         return new OAuth2ScopeTranslationEntry(this.scopePattern, this.translationKey);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "OAuth2ScopeTranslationEntry.OAuth2ScopeTranslationEntryBuilder(scopePattern="
            + this.scopePattern
            + ", translationKey="
            + this.translationKey
            + ")";
      }
   }
}
