content: {-
   Copyright 2022-23, Juspay India Pvt Ltd
  
   This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License
  
   as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program
  
   is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  
   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of
  
   the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
  -}
  
  module Environment where
  
  import qualified Data.HashMap.Strict as HM
  import qualified Data.Map.Strict as M
  import Kernel.Prelude
  import Kernel.Storage.Esqueleto.Config
  import Kernel.Storage.Hedis as Redis hiding (ttl)
  import Kernel.Storage.Hedis.AppPrefixes (publicTransportBapPrefix)
  import Kernel.Types.Cache
  import Kernel.Types.Common
  import Kernel.Types.Flow
  import Kernel.Types.Registry
  import Kernel.Utils.App (getPodName, lookupDeploymentVersion)
  import Kernel.Utils.Dhall (FromDhall)
  import Kernel.Utils.IOLogging
  import qualified Kernel.Utils.Registry as Registry
  import Kernel.Utils.Servant.Client
  import Kernel.Utils.Servant.SignatureAuth
  import Kernel.Utils.Shutdown
  import System.Environment (lookupEnv)
  import Tools.Metrics.Types
  import Tools.Streaming.Kafka.Environment
  
  data AppCfg = AppCfg
    { esqDBCfg :: EsqDBConfig,
      esqDBReplicaCfg :: EsqDBConfig,
      migrationPath :: [FilePath],
      autoMigrate :: Bool,
      hedisCfg :: HedisCfg,
      hedisClusterCfg :: HedisCfg,
      hedisNonCriticalCfg :: HedisCfg,
      hedisNonCriticalClusterCfg :: HedisCfg,
      cutOffHedisCluster :: Bool,
      hedisMigrationStage :: Bool,
      port :: Int,
      loggerConfig :: LoggerConfig,
      graceTerminationPeriod :: Seconds,
      selfId :: Text,
      selfURI :: BaseUrl,
      httpClientOptions :: HttpClientOptions,
      shortDurationRetryCfg :: RetryCfg,
      longDurationRetryCfg :: RetryCfg,
      authEntity :: AuthenticatingEntity',
      registryUrl :: BaseUrl,
      authServiceUrl :: BaseUrl,
      disableSignatureAuth :: Bool,
      hostName :: Text,
      kafkaProducerCfg :: KafkaProducerCfg,
      enableRedisLatencyLogging :: Bool,
      enablePrometheusMetricLogging :: Bool,
      internalEndPointMap :: M.Map BaseUrl BaseUrl
    }
    deriving (Generic, FromDhall)
  
  type MobileNumber = Text
  
  data AppEnv = AppEnv
    { port :: Int,
      loggerConfig :: LoggerConfig,
      graceTerminationPeriod :: Seconds,
      selfId :: Text,
      selfURI :: BaseUrl,
      httpClientOptions :: HttpClientOptions,
      shortDurationRetryCfg :: RetryCfg,
      longDurationRetryCfg :: RetryCfg,
      authEntity :: AuthenticatingEntity',
      registryUrl :: BaseUrl,
      authServiceUrl :: BaseUrl,
      disableSignatureAuth :: Bool,
      hostName :: Text,
      esqDBEnv :: EsqDBEnv,
      esqDBReplicaEnv :: EsqDBEnv,
      hedisEnv :: HedisEnv,
      hedisNonCriticalEnv :: HedisEnv,
      hedisNonCriticalClusterEnv :: HedisEnv,
      hedisClusterEnv :: HedisEnv,
      cutOffHedisCluster :: Bool,
      hedisMigrationStage :: Bool,
      isShuttingDown :: Shutdown,
      loggerEnv :: LoggerEnv,
      coreMetrics :: CoreMetricsContainer,
      kafkaProducerTools :: KafkaProducerTools,
      kafkaEnvs :: BAPKafkaEnvs,
      version :: DeploymentVersion,
      enableRedisLatencyLogging :: Bool,
      enablePrometheusMetricLogging :: Bool,
      internalEndPointHashMap :: HM.HashMap BaseUrl BaseUrl,
      requestId :: Maybe Text,
      shouldLogRequestId :: Bool,
      kafkaProducerForART :: Maybe KafkaProducerTools,
      url :: Maybe Text
    }
    deriving (Generic)
  
  buildAppEnv :: AppCfg -> IO AppEnv
  buildAppEnv AppCfg {..} = do
    -- let hedisMigrationStage = False
    podName <- getPodName
    version <- lookupDeploymentVersion
    loggerEnv <- prepareLoggerEnv loggerConfig podName
    esqDBEnv <- prepareEsqDBEnv esqDBCfg loggerEnv
    esqDBReplicaEnv <- prepareEsqDBEnv esqDBReplicaCfg loggerEnv
    coreMetrics <- registerCoreMetricsContainer
    isShuttingDown <- mkShutdown
    kafkaProducerTools <- buildKafkaProducerTools kafkaProducerCfg
    kafkaEnvs <- buildBAPKafkaEnvs
    hedisEnv <- connectHedis hedisCfg publicTransportBapPrefix
    hedisNonCriticalEnv <- connectHedis hedisNonCriticalCfg publicTransportBapPrefix
    hedisClusterEnv <-
      if cutOffHedisCluster
        then pure hedisEnv
        else connectHedisCluster hedisClusterCfg publicTransportBapPrefix
    hedisNonCriticalClusterEnv <-
      if cutOffHedisCluster
        then pure hedisNonCriticalEnv
        else connectHedisCluster hedisNonCriticalClusterCfg publicTransportBapPrefix
    let internalEndPointHashMap = HM.fromList $ M.toList internalEndPointMap
    let requestId = Nothing
    shouldLogRequestId <- fromMaybe False . (>>= readMaybe) <$> lookupEnv "SHOULD_LOG_REQUEST_ID"
    let kafkaProducerForART = Just kafkaProducerTools
    let url = Nothing
    return $ AppEnv {..}
  
  releaseAppEnv :: AppEnv -> IO ()
  releaseAppEnv AppEnv {..} = do
    releaseKafkaProducerTools kafkaProducerTools
    releaseLoggerEnv loggerEnv
    disconnectHedis hedisEnv
    disconnectHedis hedisClusterEnv
  
  type FlowHandler = FlowHandlerR AppEnv
  
  type FlowServer api = FlowServerR AppEnv api
  
  type Flow = FlowR AppEnv
  
  instance AuthenticatingEntity AppEnv where
    getSigningKey = (.authEntity.signingKey)
    getSignatureExpiry = (.authEntity.signatureExpiry)
  
  instance Registry Flow where
    registryLookup sReq = do
      registryUrl <- asks (.registryUrl)
      selfId <- asks (.selfId)
      Registry.withSubscriberCache (\req -> Registry.registryLookup registryUrl req selfId) sReq
  
  instance Cache Subscriber Flow where
    type CacheKey Subscriber = SimpleLookupRequest
    getKey = Redis.get . ("public-transport-rider-platform:registry:" <>) . lookupRequestToRedisKey
    setKey = Redis.set . ("public-transport-rider-platform:registry:" <>) . lookupRequestToRedisKey
    delKey = Redis.del . ("public-transport-rider-platform:registry:" <>) . lookupRequestToRedisKey
  
  instance CacheEx Subscriber Flow where
    setKeyEx ttl = (\k v -> Redis.setExp k v ttl.getSeconds) . ("public-transport-rider-platform:registry:" <>) . lookupRequestToRedisKey
  