package com.airlock.iam.oauth2.domain.model.client;

import com.airlock.iam.common.domain.model.jwt.signature.KeyId;
import com.airlock.iam.security.certificate.AsymmetricKeyPairUtils;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.annotation.Nullable;
import java.security.PublicKey;
import java.util.Optional;
import lombok.NonNull;

public final class OAuth2ClientPublicKey implements OAuth2ClientPublicKeyProvider {
   @NonNull
   private final PublicKey publicKey;
   @Nullable
   private final KeyId keyId;

   public static OAuth2ClientPublicKey.OAuth2ClientPublicKeyBuilder builder(String publicKey) {
      return internalBuilder().publicKey(AsymmetricKeyPairUtils.publicKeyFromPem(publicKey));
   }

   private static OAuth2ClientPublicKey.OAuth2ClientPublicKeyBuilder internalBuilder() {
      return new OAuth2ClientPublicKey.OAuth2ClientPublicKeyBuilder();
   }

   @Override
   public Optional<PublicKey> getKeyFor(Optional<KeyId> otherKeyId) {
      return this.keyId == null ? Optional.of(this.publicKey) : otherKeyId.filter(this.keyId::equals).map(k -> this.publicKey);
   }

   @SuppressFBWarnings(justification = "generated code")
   OAuth2ClientPublicKey(@NonNull PublicKey publicKey, @Nullable KeyId keyId) {
      if (publicKey == null) {
         throw new NullPointerException("publicKey is marked non-null but is null");
      }

      this.publicKey = publicKey;
      this.keyId = keyId;
   }

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   public PublicKey getPublicKey() {
      return this.publicKey;
   }

   @Nullable
   @SuppressFBWarnings(justification = "generated code")
   public KeyId getKeyId() {
      return this.keyId;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "OAuth2ClientPublicKey(publicKey=" + this.getPublicKey() + ", keyId=" + this.getKeyId() + ")";
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class OAuth2ClientPublicKeyBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private PublicKey publicKey;
      @SuppressFBWarnings(justification = "generated code")
      private KeyId keyId;

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

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2ClientPublicKey.OAuth2ClientPublicKeyBuilder publicKey(@NonNull PublicKey publicKey) {
         if (publicKey == null) {
            throw new NullPointerException("publicKey is marked non-null but is null");
         }

         this.publicKey = publicKey;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2ClientPublicKey.OAuth2ClientPublicKeyBuilder keyId(@Nullable KeyId keyId) {
         this.keyId = keyId;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2ClientPublicKey build() {
         return new OAuth2ClientPublicKey(this.publicKey, this.keyId);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "OAuth2ClientPublicKey.OAuth2ClientPublicKeyBuilder(publicKey=" + this.publicKey + ", keyId=" + this.keyId + ")";
      }
   }
}
