package com.airlock.iam.flow.shared.domain.model.matrix;

import com.airlock.iam.base.api.domain.model.mapification.Mapifiable;
import com.airlock.iam.base.api.domain.model.mapification.MapifiedValues;
import com.airlock.iam.base.domain.model.mapification.MapifiedValuesBuilder;
import com.airlock.iam.flow.api.domain.model.step.StepResultDataCollection;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import lombok.NonNull;

public final class MatrixChallenge implements StepResultDataCollection<String>, Mapifiable {
   private static final String CHALLENGES_KEY = "challenges";
   private static final String LIST_ID_KEY = "listId";
   @Nullable
   private final String listId;
   @NonNull
   private final List<String> challenges;

   @Override
   public Collection<String> getElements() {
      return this.challenges;
   }

   @Override
   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues()
         .putIfPresent("listId", Optional.ofNullable(this.listId))
         .putIfNotEmpty("challenges", this.challenges.stream().toList())
         .build();
   }

   public static MatrixChallenge demapify(MapifiedValues values) {
      return new MatrixChallenge(values.<String>getOptional("listId").orElse(null), values.getList("challenges"));
   }

   @SuppressFBWarnings(justification = "generated code")
   MatrixChallenge(@Nullable String listId, @NonNull List<String> challenges) {
      if (challenges == null) {
         throw new NullPointerException("challenges is marked non-null but is null");
      }

      this.listId = listId;
      this.challenges = challenges;
   }

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

   @Nullable
   @SuppressFBWarnings(justification = "generated code")
   public String getListId() {
      return this.listId;
   }

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   public List<String> getChallenges() {
      return this.challenges;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "MatrixChallenge(listId=" + this.getListId() + ", challenges=" + this.getChallenges() + ")";
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class MatrixChallengeBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private String listId;
      @SuppressFBWarnings(justification = "generated code")
      private ArrayList<String> challenges;

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

      @SuppressFBWarnings(justification = "generated code")
      public MatrixChallenge.MatrixChallengeBuilder listId(@Nullable String listId) {
         this.listId = listId;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public MatrixChallenge.MatrixChallengeBuilder challenge(String challenge) {
         if (this.challenges == null) {
            this.challenges = new ArrayList<>();
         }

         this.challenges.add(challenge);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public MatrixChallenge.MatrixChallengeBuilder challenges(Collection<? extends String> challenges) {
         if (challenges == null) {
            throw new NullPointerException("challenges cannot be null");
         }

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

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

      @SuppressFBWarnings(justification = "generated code")
      public MatrixChallenge.MatrixChallengeBuilder clearChallenges() {
         if (this.challenges != null) {
            this.challenges.clear();
         }

         return this;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "MatrixChallenge.MatrixChallengeBuilder(listId=" + this.listId + ", challenges=" + this.challenges + ")";
      }
   }
}
