content: module Config.Env where
  
  import Constants
  import Data.Text as T
  import EulerHS.Prelude
  import System.Environment as SE
  import Types.Config
  
  getDBSyncStream :: IO [Char]
  getDBSyncStream = fromMaybe "driver-db-sync-stream" <$> SE.lookupEnv dbSyncStreamEnvKey
  
  gracefulShutDownPeriodInMs :: IO Int
  gracefulShutDownPeriodInMs = (1000000 *) . fromMaybe defaultGracefulShutDownPeriodInMs . (>>= readMaybe) <$> SE.lookupEnv gracefulShutDownPeriodSecondsEnvKey
  
  getStreamBlockTime :: IO (Maybe Integer)
  getStreamBlockTime = (\ms -> if ms == 0 then Nothing else Just ms) <$> (fromMaybe 100 . (>>= readMaybe) <$> SE.lookupEnv streamBlockTimeEnvKey)
  
  hostname :: Text
  hostname = T.pack "" -- Env.getApplicationHostname
  
  getRetryDelay :: IO Int
  getRetryDelay = fromMaybe 1000000 . (>>= readMaybe) <$> SE.lookupEnv dbFailureRetryDelayEnvKey
  
  getDrainerRetryDelay :: IO Int
  getDrainerRetryDelay = fromMaybe 5000000 . (>>= readMaybe) <$> SE.lookupEnv drainerFailureRetryDelayEnvKey
  
  getMaxRetries :: IO Int
  getMaxRetries = fromMaybe 3 . (>>= readMaybe) <$> SE.lookupEnv maxDbFailureRetries
  
  getDrainerExecutionDelay :: IO Int
  getDrainerExecutionDelay = fromMaybe 0 . (>>= readMaybe) <$> SE.lookupEnv drainerExecutionDelayEnvKey
  
  defaultDBSyncConfig :: DBSyncConfig
  defaultDBSyncConfig =
    DBSyncConfig
      { _emptyRetry = 50,
        _rateLimitN = 200,
        _rateLimitWindow = 100,
        _streamReadCount = 1000 -- 1000
      }
  
  getThreadPerPodCount :: IO Int
  getThreadPerPodCount = fromMaybe 0 . (>>= readMaybe) <$> SE.lookupEnv threadPerPodCount
  
  isPushToKafka :: IO Bool
  isPushToKafka = fromMaybe False . (>>= readMaybe) <$> SE.lookupEnv pushToKafkaEnvKey
  
  -- DB Connection Recovery Configuration
  getDbConnectionRetryMaxAttempts :: IO Int
  getDbConnectionRetryMaxAttempts = fromMaybe defaultDbConnectionRetryMaxAttempts . (>>= readMaybe) <$> SE.lookupEnv dbConnectionRetryMaxAttempts
  
  getDbConnectionRetryDelay :: IO Int
  getDbConnectionRetryDelay = fromMaybe defaultDbConnectionRetryDelay . (>>= readMaybe) <$> SE.lookupEnv dbConnectionRetryDelayEnvKey
  
  -- Batch Create Configuration
  getBatchCreateEnabled :: IO Bool
  getBatchCreateEnabled = fromMaybe False . (>>= readMaybe) <$> SE.lookupEnv "BATCHED_CREATE_ENABLED"
  
  getInsertBatchSize :: IO Int
  getInsertBatchSize = fromMaybe 50 . (>>= readMaybe) <$> SE.lookupEnv "INSERT_BATCH_SIZE"
  