package com.airlock.iam.factor.domain.model.airlock2fa;

import com.airlock.iam.airlock2fa.api.domain.model.Airlock2FAAccountId;
import com.airlock.iam.airlock2fa.api.domain.model.Airlock2FADeviceId;
import com.airlock.iam.base.misc.log.LogUtil;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.annotation.Nullable;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.NonNull;

public final class Airlock2FAAuthLogData {
   @Nullable
   private final Airlock2FAAccountId accountId;
   @Nullable
   private final Airlock2FASessionId sessionId;
   @Nullable
   private final Airlock2FASessionId fallbackSessionId;
   @Nullable
   private final Airlock2FADeviceId deviceId;
   @Nullable
   private final Airlock2FAUserPresenceVerification userPresenceVerification;

   public static Airlock2FAAuthLogData airlock2FAAuthLogData() {
      return new Airlock2FAAuthLogData(null, null, null, null, null);
   }

   public static Airlock2FAAuthLogData airlock2FAAuthLogData(@NonNull Airlock2FAAccountId accountId) {
      if (accountId == null) {
         throw new NullPointerException("accountId is marked non-null but is null");
      } else {
         return new Airlock2FAAuthLogData(accountId, null, null, null, null);
      }
   }

   public Airlock2FAAuthLogData withDeviceIdIfPresent(Optional<Airlock2FADeviceId> deviceId) {
      return deviceId.map(this::withDeviceId).orElse(this);
   }

   public Airlock2FAAuthLogData withSessionIdIfPresent(Optional<Airlock2FASessionId> sessionId) {
      return sessionId.map(this::withSessionId).orElse(this);
   }

   public Airlock2FAAuthLogData withFallbackSessionIdIfPresent(Optional<Airlock2FASessionId> fallbackSessionId) {
      return fallbackSessionId.map(this::withFallbackSessionId).orElse(this);
   }

   public Airlock2FAAuthLogData withUserPresenceVerificationIfPresent(Optional<Airlock2FAUserPresenceVerification> userPresenceVerification) {
      return userPresenceVerification.map(this::withUserPresenceVerification).orElse(this);
   }

   public Airlock2FAAuthLogData withAccountIdIfPresent(Optional<Airlock2FAAccountId> accountId) {
      return accountId.map(this::withAccountId).orElse(this);
   }

   @Override
   public String toString() {
      return Stream.of(
            Optional.ofNullable(this.accountId).stream().map(x -> "accountId = " + LogUtil.logSafeQuote(x.getId())),
            Optional.ofNullable(this.sessionId).stream().map(x -> "sessionId = " + LogUtil.logSafeQuote(x.getSessionId())),
            Optional.ofNullable(this.fallbackSessionId).stream().map(x -> "fallbackSessionId = " + LogUtil.logSafeQuote(x.getSessionId())),
            Optional.ofNullable(this.deviceId).stream().map(x -> "deviceId = " + LogUtil.logSafeQuote(x.getId())),
            Optional.ofNullable(this.userPresenceVerification).stream().map(x -> "userPresenceVerification = " + LogUtil.logSafeQuote(x.value()))
         )
         .flatMap(Function.identity())
         .collect(Collectors.joining(", ", "(", ")"));
   }

   @SuppressFBWarnings(justification = "generated code")
   private Airlock2FAAuthLogData(
      @Nullable Airlock2FAAccountId accountId,
      @Nullable Airlock2FASessionId sessionId,
      @Nullable Airlock2FASessionId fallbackSessionId,
      @Nullable Airlock2FADeviceId deviceId,
      @Nullable Airlock2FAUserPresenceVerification userPresenceVerification
   ) {
      this.accountId = accountId;
      this.sessionId = sessionId;
      this.fallbackSessionId = fallbackSessionId;
      this.deviceId = deviceId;
      this.userPresenceVerification = userPresenceVerification;
   }

   @SuppressFBWarnings(justification = "generated code")
   public Airlock2FAAuthLogData withAccountId(@Nullable Airlock2FAAccountId accountId) {
      return this.accountId == accountId
         ? this
         : new Airlock2FAAuthLogData(accountId, this.sessionId, this.fallbackSessionId, this.deviceId, this.userPresenceVerification);
   }

   @SuppressFBWarnings(justification = "generated code")
   public Airlock2FAAuthLogData withSessionId(@Nullable Airlock2FASessionId sessionId) {
      return this.sessionId == sessionId
         ? this
         : new Airlock2FAAuthLogData(this.accountId, sessionId, this.fallbackSessionId, this.deviceId, this.userPresenceVerification);
   }

   @SuppressFBWarnings(justification = "generated code")
   public Airlock2FAAuthLogData withFallbackSessionId(@Nullable Airlock2FASessionId fallbackSessionId) {
      return this.fallbackSessionId == fallbackSessionId
         ? this
         : new Airlock2FAAuthLogData(this.accountId, this.sessionId, fallbackSessionId, this.deviceId, this.userPresenceVerification);
   }

   @SuppressFBWarnings(justification = "generated code")
   public Airlock2FAAuthLogData withDeviceId(@Nullable Airlock2FADeviceId deviceId) {
      return this.deviceId == deviceId
         ? this
         : new Airlock2FAAuthLogData(this.accountId, this.sessionId, this.fallbackSessionId, deviceId, this.userPresenceVerification);
   }

   @SuppressFBWarnings(justification = "generated code")
   public Airlock2FAAuthLogData withUserPresenceVerification(@Nullable Airlock2FAUserPresenceVerification userPresenceVerification) {
      return this.userPresenceVerification == userPresenceVerification
         ? this
         : new Airlock2FAAuthLogData(this.accountId, this.sessionId, this.fallbackSessionId, this.deviceId, userPresenceVerification);
   }

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

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