package com.airlock.iam.techclientreg.application.configuration.registration;

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.RegexListProperty;
import com.airlock.iam.techclientreg.api.application.configuration.oauth2.OAuth2ClientMetadataProcessorConfig;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

@Plugin(
   description = "Processes the \"scope\" metadata attribute. The value from the request is interpreted as a space-separated list of scopes. All scopes that match against at least one of the allowed scope patterns are accepted, the others are ignored.",
   displayName = "Scope Processor"
)
@PluginLicenseRestriction(moduleAffiliation = "TechClientRegistration")
public class ScopeProcessorConfig implements OAuth2ClientMetadataProcessorConfig {
   @RegexListProperty(
      description = "A list of regular expressions that define the allowed scopes. Requested scopes not matching any of these patterns are ignored.<p>The default value makes sure the scopes do not include characters outside the set <code>%x20-21 / %x23-5B / %x5D-7E</code> (NQCHAR). For more information see <a href=\"https://datatracker.ietf.org/doc/html/rfc6749#appendix-A\" target=\"_blank\">RFC 6749</a>.</p>",
      optional = true,
      minElements = 1,
      exampleElements = {"scope1", "[a-zA-Z0-9_.-]+"},
      defaultValue = "[\\x21\\x23-\\x5B\\x5D-\\x7E]+"
   )
   private List<Pattern> allowedScopes;

   public List<Pattern> getAllowedScopes() {
      return this.allowedScopes;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public ScopeProcessorConfig(List<Pattern> allowedScopes) {
      this.allowedScopes = allowedScopes;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public static class ScopeProcessorConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private ArrayList<Pattern> allowedScopes;

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

      @SuppressFBWarnings(justification = "generated code")
      public ScopeProcessorConfig.ScopeProcessorConfigBuilder allowedScope(Pattern allowedScope) {
         if (this.allowedScopes == null) {
            this.allowedScopes = new ArrayList<>();
         }

         this.allowedScopes.add(allowedScope);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ScopeProcessorConfig.ScopeProcessorConfigBuilder allowedScopes(Collection<? extends Pattern> allowedScopes) {
         if (allowedScopes == null) {
            throw new NullPointerException("allowedScopes cannot be null");
         }

         if (this.allowedScopes == null) {
            this.allowedScopes = new ArrayList<>();
         }

         this.allowedScopes.addAll(allowedScopes);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ScopeProcessorConfig.ScopeProcessorConfigBuilder clearAllowedScopes() {
         if (this.allowedScopes != null) {
            this.allowedScopes.clear();
         }

         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ScopeProcessorConfig build() {
         return new ScopeProcessorConfig(switch (this.allowedScopes == null ? 0 : this.allowedScopes.size()) {
            case 0 -> Collections.emptyList();
            case 1 -> Collections.singletonList(this.allowedScopes.get(0));
            default -> Collections.unmodifiableList(new ArrayList<>(this.allowedScopes));
         });
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "ScopeProcessorConfig.ScopeProcessorConfigBuilder(allowedScopes=" + this.allowedScopes + ")";
      }
   }
}
