package com.airlock.iam.flow.shared.domain.model.attributes;

import com.airlock.iam.base.api.domain.model.exception.FailureException;
import com.airlock.iam.base.api.domain.model.mapification.MapifiedValues;
import com.airlock.iam.base.domain.model.mapification.MapifiedValuesBuilder;
import com.airlock.iam.flow.shared.api.domain.model.session.UserSessionScopedFlowAttribute;
import com.airlock.iam.flow.shared.domain.model.uitenantid.UiTenantId;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Map;
import java.util.Optional;

public final class UiTenantIdAttribute implements UserSessionScopedFlowAttribute<UiTenantIdAttribute> {
   private static final String TENANT_KEY = "uiTenantId";
   private final UiTenantId tenantId;

   public static UiTenantIdAttribute uiTenantIdAttribute(UiTenantId uiTenantId) {
      return new UiTenantIdAttribute(uiTenantId);
   }

   public UiTenantIdAttribute mergeWith(UiTenantIdAttribute newAttribute) {
      return newAttribute;
   }

   public UiTenantIdAttribute copyAttribute() {
      return uiTenantIdAttribute(this.tenantId);
   }

   @Override
   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues().put("uiTenantId", this.tenantId.uiTenantId()).build();
   }

   public static UiTenantIdAttribute demapify(MapifiedValues values) {
      return new UiTenantIdAttribute(values.simpleValue("uiTenantId", UiTenantId::new));
   }

   public static UiTenantIdAttribute deserializeSessionAttribute(Map<String, String> rawValues) {
      return new UiTenantIdAttribute(
         Optional.ofNullable(rawValues.get("uiTenantId")).map(UiTenantId::new).orElseThrow(() -> new FailureException("Failed to deserialize uiTenantId"))
      );
   }

   @SuppressFBWarnings(justification = "generated code")
   public UiTenantId getTenantId() {
      return this.tenantId;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "UiTenantIdAttribute(tenantId=" + this.getTenantId() + ")";
   }

   @SuppressFBWarnings(justification = "generated code")
   private UiTenantIdAttribute(UiTenantId tenantId) {
      this.tenantId = tenantId;
   }
}
