package com.airlock.iam.flow.shared.infrastructure.service.newdevice;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public record LoginFromNewDeviceParsingFailure(LoginFromNewDeviceParsingFailure.FailureType type, String reason) {
   public String getMessage() {
      return String.format("%s. Reason: %s", this.type.description(), this.reason);
   }

   public static LoginFromNewDeviceParsingFailure of(LoginFromNewDeviceParsingFailure.FailureType type, String reason) {
      return new LoginFromNewDeviceParsingFailure(type, reason);
   }

   public static LoginFromNewDeviceParsingFailure of(LoginFromNewDeviceParsingFailure.FailureType type, Throwable e) {
      return new LoginFromNewDeviceParsingFailure(type, e.getMessage());
   }

   public static LoginFromNewDeviceParsingFailure of(LoginFromNewDeviceParsingFailure.FailureType type, String reason, Throwable e) {
      return new LoginFromNewDeviceParsingFailure(type, String.format("%s. %s", reason, e));
   }

   public enum FailureType {
      BAD_JWT("Bad JWT"),
      WRONG_TYPE_HEADER("Wrong \"typ\" header"),
      WRONG_ENCRYPTION_ALGORITHM("Wrong encryption algorithm"),
      DECRYPTION_ERROR("Decryption failed"),
      MAPPING_ERROR("Mapping JSON to object of desired type failed");

      private final String description;

      FailureType(String description) {
         this.description = description;
      }

      @SuppressFBWarnings(justification = "generated code")
      public String description() {
         return this.description;
      }
   }
}
