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

import com.airlock.iam.common.api.application.configuration.valueprovider.DateAndTimeValueProviderConfig;
import com.airlock.iam.flow.api.application.configuration.valueprovider.ValueMapProviderConfig;
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 = "Defines a date and time value to be provided from a Value Map Provider and a key. If the key is not present, an empty value (null) is returned.\n<p>If the provided value is a string, the value provider will automatically attempt to convert it to a date and time representation.</p>\n<p>For this, the provided date-time string must be in ISO8601 format (e.g. 2025-10-27T10:02:00Z).</p>\n<p>If the value is an incompatible type, or converting the string is impossible, an error is thrown.</p>",
   displayName = "Date And Time From Map Value Provider"
)
public final class DateAndTimeFromMapValueProviderConfig implements DateAndTimeValueProviderConfig {
   @PluginListProperty(
      description = "Value maps from which the date and time to be provided is selected. If a key occurs in more than one value map, the providers further down in the list take precedence.",
      optional = false
   )
   private List<ValueMapProviderConfig> valueMaps;
   @StringProperty(
      description = "Case-sensitive key to select the date and time in the value map.",
      exampleValues = {"registration_date", "auth_migration_date"},
      optional = false
   )
   private String key;

   public List<ValueMapProviderConfig> getValueMaps() {
      return this.valueMaps;
   }

   public String getKey() {
      return this.key;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public DateAndTimeFromMapValueProviderConfig(List<ValueMapProviderConfig> valueMaps, String key) {
      this.valueMaps = valueMaps;
      this.key = key;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class DateAndTimeFromMapValueProviderConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private ArrayList<ValueMapProviderConfig> valueMaps;
      @SuppressFBWarnings(justification = "generated code")
      private String key;

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

      @SuppressFBWarnings(justification = "generated code")
      public DateAndTimeFromMapValueProviderConfig.DateAndTimeFromMapValueProviderConfigBuilder valueMap(ValueMapProviderConfig valueMap) {
         if (this.valueMaps == null) {
            this.valueMaps = new ArrayList<>();
         }

         this.valueMaps.add(valueMap);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public DateAndTimeFromMapValueProviderConfig.DateAndTimeFromMapValueProviderConfigBuilder valueMaps(
         Collection<? extends ValueMapProviderConfig> valueMaps
      ) {
         if (valueMaps == null) {
            throw new NullPointerException("valueMaps cannot be null");
         }

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

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

      @SuppressFBWarnings(justification = "generated code")
      public DateAndTimeFromMapValueProviderConfig.DateAndTimeFromMapValueProviderConfigBuilder clearValueMaps() {
         if (this.valueMaps != null) {
            this.valueMaps.clear();
         }

         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public DateAndTimeFromMapValueProviderConfig.DateAndTimeFromMapValueProviderConfigBuilder key(String key) {
         this.key = key;
         return this;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "DateAndTimeFromMapValueProviderConfig.DateAndTimeFromMapValueProviderConfigBuilder(valueMaps=" + this.valueMaps + ", key=" + this.key + ")";
      }
   }
}
