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/>.
  -}
  {-# OPTIONS_GHC -Wno-deprecations #-}
  
  module Storage.CachedQueries.Merchant.DriverIntelligentPoolConfig
    {-# WARNING
      "This module contains direct calls to the table and redis. \
    \ But most likely you need a version from Cac with inMem results feature."
      #-}
    ( create,
      clearCache,
      update,
      getDriverIntelligentPoolConfigFromDB,
    )
  where
  
  import Data.Coerce (coerce)
  import Data.Text as Text
  import Domain.Types.Common
  import Domain.Types.DriverIntelligentPoolConfig
  import Domain.Types.MerchantOperatingCity
  import Kernel.Prelude
  import qualified Kernel.Storage.Hedis as Hedis
  import Kernel.Types.Id
  import Kernel.Utils.Common
  import Storage.Beam.SystemConfigs ()
  import qualified Storage.Queries.DriverIntelligentPoolConfig as Queries
  
  create :: (MonadFlow m, CacheFlow m r, EsqDBFlow m r) => DriverIntelligentPoolConfig -> m ()
  create = Queries.create
  
  getDriverIntelligentPoolConfigFromDB :: (CacheFlow m r, EsqDBFlow m r) => Id MerchantOperatingCity -> m (Maybe DriverIntelligentPoolConfig)
  getDriverIntelligentPoolConfigFromDB id =
    Hedis.withCrossAppRedis (Hedis.safeGet $ makeMerchantOpCityIdKey id) >>= \case
      Just a -> return . Just $ coerce @(DriverIntelligentPoolConfigD 'Unsafe) @DriverIntelligentPoolConfig a
      Nothing -> flip whenJust cacheDriverIntelligentPoolConfig /=<< Queries.findByMerchantOpCityId id
  
  cacheDriverIntelligentPoolConfig :: CacheFlow m r => DriverIntelligentPoolConfig -> m ()
  cacheDriverIntelligentPoolConfig cfg = do
    expTime <- fromIntegral <$> asks (.cacheConfig.configsExpTime)
    let merchantIdKey = makeMerchantOpCityIdKey cfg.merchantOperatingCityId
    Hedis.withCrossAppRedis $ Hedis.setExp merchantIdKey (coerce @DriverIntelligentPoolConfig @(DriverIntelligentPoolConfigD 'Unsafe) cfg) expTime
  
  makeMerchantOpCityIdKey :: Id MerchantOperatingCity -> Text
  makeMerchantOpCityIdKey id = "driver-offer:CachedQueries:DriverIntelligentPoolConfig:MerchantOperatingCityId-" <> id.getId
  
  -- Call it after any update
  clearCache :: Hedis.HedisFlow m r => Id MerchantOperatingCity -> m ()
  clearCache = Hedis.withCrossAppRedis . Hedis.del . makeMerchantOpCityIdKey
  
  update :: (MonadFlow m, CacheFlow m r, EsqDBFlow m r) => DriverIntelligentPoolConfig -> m ()
  update = Queries.update
  