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

import com.airlock.iam.flow.api.application.configuration.tag.TagConfig;
import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.property.PluginListProperty;
import com.airlock.iam.plugin.api.annotation.property.StringProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Plugin(description = "Mapping from roles removed by Airlock Gateway to tags to be removed in Airlock IAM.")
public final class RemovedRolesMapping {
   @StringProperty(
      description = "Name of the role removed by Airlock Gateway. This is the role that triggers the removal of the tags configured below.",
      optional = false
   )
   private String roleName;
   @PluginListProperty(
      description = "List of tags that will be removed whenever the role configured above has been removed by Airlock Gateway. Tag timeouts are ignored, all tags with the same name as the tags configured here are removed.",
      optional = false,
      minElements = 1
   )
   private List<TagConfig> tags;

   public String getRoleName() {
      return this.roleName;
   }

   public List<TagConfig> getTags() {
      return this.tags;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public RemovedRolesMapping(String roleName, List<TagConfig> tags) {
      this.roleName = roleName;
      this.tags = tags;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public static class RemovedRolesMappingBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private String roleName;
      @SuppressFBWarnings(justification = "generated code")
      private ArrayList<TagConfig> tags;

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

      @SuppressFBWarnings(justification = "generated code")
      public RemovedRolesMapping.RemovedRolesMappingBuilder roleName(String roleName) {
         this.roleName = roleName;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public RemovedRolesMapping.RemovedRolesMappingBuilder tag(TagConfig tag) {
         if (this.tags == null) {
            this.tags = new ArrayList<>();
         }

         this.tags.add(tag);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public RemovedRolesMapping.RemovedRolesMappingBuilder tags(Collection<? extends TagConfig> tags) {
         if (tags == null) {
            throw new NullPointerException("tags cannot be null");
         }

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

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

      @SuppressFBWarnings(justification = "generated code")
      public RemovedRolesMapping.RemovedRolesMappingBuilder clearTags() {
         if (this.tags != null) {
            this.tags.clear();
         }

         return this;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "RemovedRolesMapping.RemovedRolesMappingBuilder(roleName=" + this.roleName + ", tags=" + this.tags + ")";
      }
   }
}
