package com.airlock.iam.common.infrastructure.state.redis;

import com.airlock.iam.base.api.domain.model.exception.FailureException;
import com.airlock.iam.common.domain.service.state.StateRepositoryKey;

public class CommonRedisLegacyKeyHelper implements RedisLegacyKeyHelper {
   private final String legacySessionSuffix;

   public CommonRedisLegacyKeyHelper(String legacySessionSuffix) {
      this.legacySessionSuffix = legacySessionSuffix;
   }

   @Override
   public String toLegacyKey(StateRepositoryKey key) {
      if (key.hasStateEntryType(StateRepositoryKey.StandardStateEntryType.SESSION)) {
         return key.id() + this.legacySessionSuffix;
      } else if (key.hasStateEntryType(StateRepositoryKey.StandardStateEntryType.USER)) {
         return key.id();
      } else {
         throw new FailureException("Unexpected state entry type to be transformed to legacy key format for key: " + key.value());
      }
   }

   @Override
   public boolean mustTransformToLegacyKey(StateRepositoryKey key) {
      return key.hasStateEntryType(StateRepositoryKey.StandardStateEntryType.SESSION) || key.hasStateEntryType(StateRepositoryKey.StandardStateEntryType.USER);
   }
}
