package com.airlock.iam.authentication.domain.model.processor;

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.MapificationUtils;
import com.airlock.iam.base.domain.model.mapification.MapifiedValuesBuilder;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.annotation.Nullable;
import java.time.Instant;
import java.util.Date;
import java.util.Optional;

public final class LatestAuthentication implements Mapifiable {
   private static final String ATTEMPT_KEY = "attemptAt";
   private static final String SUCCESS_KEY = "successful";
   private final Optional<Instant> attemptAt;
   private final Optional<Boolean> successful;

   public LatestAuthentication(@Nullable Date latestAuthenticationAttempt, @Nullable Date latestSuccessfulAuthenticationAttempt) {
      this.attemptAt = Optional.ofNullable(latestAuthenticationAttempt).map(Date::toInstant);
      this.successful = this.attemptAt.map(att -> isSameLoginAttempt(att, toNullableInstant(latestSuccessfulAuthenticationAttempt)));
   }

   private static Instant toNullableInstant(Date date) {
      return date != null ? date.toInstant() : null;
   }

   private static boolean isSameLoginAttempt(Instant firstDate, Instant secondDate) {
      return firstDate != null && secondDate != null ? Math.abs(firstDate.toEpochMilli() - secondDate.toEpochMilli()) <= 2000L : false;
   }

   @Override
   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues()
         .putIfPresent("attemptAt", this.attemptAt.map(MapificationUtils::mapifyInstant))
         .putIfPresent("successful", this.successful)
         .build();
   }

   public static LatestAuthentication demapify(MapifiedValues values) {
      return new LatestAuthentication(values.demapifyOptional("attemptAt", MapificationUtils::demapifyInstant), values.getOptional("successful"));
   }

   @SuppressFBWarnings(justification = "generated code")
   public Optional<Instant> getAttemptAt() {
      return this.attemptAt;
   }

   @SuppressFBWarnings(justification = "generated code")
   public Optional<Boolean> getSuccessful() {
      return this.successful;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "LatestAuthentication(attemptAt=" + this.getAttemptAt() + ", successful=" + this.getSuccessful() + ")";
   }

   @SuppressFBWarnings(justification = "generated code")
   public LatestAuthentication(Optional<Instant> attemptAt, Optional<Boolean> successful) {
      this.attemptAt = attemptAt;
      this.successful = successful;
   }
}
