package com.airlock.iam.fido.api.domain.model;

import com.airlock.iam.base.api.domain.model.identity.Identity;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.annotation.Nullable;
import java.util.UUID;
import lombok.NonNull;
import org.apache.commons.lang3.StringUtils;

public final class FidoCredentialId implements Identity<FidoCredential> {
   private final UUID id;

   private FidoCredentialId(@NonNull UUID id) {
      if (id == null) {
         throw new NullPointerException("id is marked non-null but is null");
      }

      this.id = id;
   }

   public static FidoCredentialId newCredentialId() {
      return new FidoCredentialId(UUID.randomUUID());
   }

   public static FidoCredentialId credentialId(String id) {
      return new FidoCredentialId(UUID.fromString(id));
   }

   public static boolean isValid(@Nullable String id) {
      if (StringUtils.isBlank(id)) {
         return false;
      }

      try {
         credentialId(id);
         return true;
      } catch (IllegalArgumentException e) {
         return false;
      }
   }

   @Override
   public String toString() {
      return this.id.toString();
   }

   @Override
   public String asString() {
      return this.id.toString();
   }

   @SuppressFBWarnings(justification = "generated code")
   public UUID getId() {
      return this.id;
   }

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

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