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

import com.airlock.iam.oauth2.api.domain.model.clientauthentication.OAuth2AuthClientId;
import com.airlock.iam.oauth2.api.domain.model.clientauthentication.OAuth2ClientAuthenticationPayload;
import com.airlock.iam.oauth2.api.domain.model.clientauthentication.OAuth2ClientAuthenticationVerificationMethod;
import com.airlock.iam.oauth2.domain.model.clientauthentication.method.KnownClientAuthenticationMethod;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.security.cert.X509Certificate;
import java.util.Optional;
import lombok.NonNull;

public final class OAuth2ClientCertificateAuthenticationPayload implements OAuth2ClientAuthenticationPayload {
   @NonNull
   private final OAuth2AuthClientId clientId;
   @NonNull
   private final X509Certificate certificate;

   @Override
   public Optional<OAuth2AuthClientId> clientId() {
      return Optional.of(this.clientId);
   }

   @Override
   public OAuth2ClientAuthenticationVerificationMethod method() {
      return KnownClientAuthenticationMethod.MTLS_CERTIFICATE;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   private OAuth2ClientCertificateAuthenticationPayload(@NonNull OAuth2AuthClientId clientId, @NonNull X509Certificate certificate) {
      if (clientId == null) {
         throw new NullPointerException("clientId is marked non-null but is null");
      }

      if (certificate == null) {
         throw new NullPointerException("certificate is marked non-null but is null");
      }

      this.clientId = clientId;
      this.certificate = certificate;
   }

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

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

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   public X509Certificate certificate() {
      return this.certificate;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class OAuth2ClientCertificateAuthenticationPayloadBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private OAuth2AuthClientId clientId;
      @SuppressFBWarnings(justification = "generated code")
      private X509Certificate certificate;

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

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2ClientCertificateAuthenticationPayload.OAuth2ClientCertificateAuthenticationPayloadBuilder clientId(@NonNull OAuth2AuthClientId clientId) {
         if (clientId == null) {
            throw new NullPointerException("clientId is marked non-null but is null");
         }

         this.clientId = clientId;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2ClientCertificateAuthenticationPayload.OAuth2ClientCertificateAuthenticationPayloadBuilder certificate(@NonNull X509Certificate certificate) {
         if (certificate == null) {
            throw new NullPointerException("certificate is marked non-null but is null");
         }

         this.certificate = certificate;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2ClientCertificateAuthenticationPayload build() {
         return new OAuth2ClientCertificateAuthenticationPayload(this.clientId, this.certificate);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "OAuth2ClientCertificateAuthenticationPayload.OAuth2ClientCertificateAuthenticationPayloadBuilder(clientId="
            + this.clientId
            + ", certificate="
            + this.certificate
            + ")";
      }
   }
}
