package com.airlock.iam.apipolicyservice.domain.model;

import com.airlock.iam.base.api.domain.model.Entity;
import com.airlock.iam.base.api.domain.model.identity.Identity;
import com.airlock.iam.common.domain.model.identity.DefaultStringIdentity;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import lombok.NonNull;

public final class ApiPolicyServiceSubscription implements Entity<ApiPolicyServiceSubscription> {
   @NonNull
   private final Identity<ApiPolicyServiceSubscription> id;
   private final Set<ApiPolicyServicePlanUsage> plans;

   public static Identity<ApiPolicyServiceSubscription> subscriptionId(String id) {
      return DefaultStringIdentity.identity(id, ApiPolicyServiceSubscription.class);
   }

   @SuppressFBWarnings(justification = "generated code")
   ApiPolicyServiceSubscription(@NonNull Identity<ApiPolicyServiceSubscription> id, Set<ApiPolicyServicePlanUsage> plans) {
      if (id == null) {
         throw new NullPointerException("id is marked non-null but is null");
      }

      this.id = id;
      this.plans = plans;
   }

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

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   public Identity<ApiPolicyServiceSubscription> getId() {
      return this.id;
   }

   @SuppressFBWarnings(justification = "generated code")
   public Set<ApiPolicyServicePlanUsage> getPlans() {
      return this.plans;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof ApiPolicyServiceSubscription other)) {
         return false;
      } else {
         Object this$id = this.id;
         Object other$id = other.id;
         if (this$id == null ? other$id == null : this$id.equals(other$id)) {
            Object this$plans = this.plans;
            Object other$plans = other.plans;
            return this$plans == null ? other$plans == null : this$plans.equals(other$plans);
         } else {
            return false;
         }
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public int hashCode() {
      int PRIME = 59;
      int result = 1;
      Object $id = this.id;
      result = result * 59 + ($id == null ? 43 : $id.hashCode());
      Object $plans = this.plans;
      return result * 59 + ($plans == null ? 43 : $plans.hashCode());
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "ApiPolicyServiceSubscription(id=" + this.getId() + ", plans=" + this.getPlans() + ")";
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class ApiPolicyServiceSubscriptionBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private Identity<ApiPolicyServiceSubscription> id;
      @SuppressFBWarnings(justification = "generated code")
      private ArrayList<ApiPolicyServicePlanUsage> plans;

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

      @SuppressFBWarnings(justification = "generated code")
      public ApiPolicyServiceSubscription.ApiPolicyServiceSubscriptionBuilder id(@NonNull Identity<ApiPolicyServiceSubscription> id) {
         if (id == null) {
            throw new NullPointerException("id is marked non-null but is null");
         }

         this.id = id;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ApiPolicyServiceSubscription.ApiPolicyServiceSubscriptionBuilder plan(ApiPolicyServicePlanUsage plan) {
         if (this.plans == null) {
            this.plans = new ArrayList<>();
         }

         this.plans.add(plan);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ApiPolicyServiceSubscription.ApiPolicyServiceSubscriptionBuilder plans(Collection<? extends ApiPolicyServicePlanUsage> plans) {
         if (plans == null) {
            throw new NullPointerException("plans cannot be null");
         }

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

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

      @SuppressFBWarnings(justification = "generated code")
      public ApiPolicyServiceSubscription.ApiPolicyServiceSubscriptionBuilder clearPlans() {
         if (this.plans != null) {
            this.plans.clear();
         }

         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ApiPolicyServiceSubscription build() {
         return new ApiPolicyServiceSubscription(
            this.id,
            switch (this.plans == null ? 0 : this.plans.size()) {
               case 0 -> Collections.emptySet();
               case 1 -> Collections.singleton(this.plans.get(0));
               default -> {
                  Set<ApiPolicyServicePlanUsage> plans = new LinkedHashSet<>(
                     this.plans.size() < 1073741824 ? 1 + this.plans.size() + (this.plans.size() - 3) / 3 : Integer.MAX_VALUE
                  );
                  plans.addAll(this.plans);
                  yield Collections.unmodifiableSet(plans);
               }
            }
         );
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "ApiPolicyServiceSubscription.ApiPolicyServiceSubscriptionBuilder(id=" + this.id + ", plans=" + this.plans + ")";
      }
   }
}
