module Hasura.RQL.DDL.Schema.Cache.Config
  ( -- * static config
    CacheStaticConfig (..),
    HasCacheStaticConfig (..),

    -- * dynamic config
    CacheDynamicConfig (..),
  )
where

import Hasura.Logging (Hasura, Logger)
import Hasura.Prelude
import Hasura.RQL.Types.BackendType
import Hasura.RQL.Types.Common (SQLGenCtx)
import Hasura.RQL.Types.Metadata (MetadataDefaults)
import Hasura.RQL.Types.NamingCase (NamingCase)
import Hasura.RQL.Types.Schema.Options qualified as Options
import Hasura.Server.Types

--------------------------------------------------------------------------------
-- static config

-- | This type aggregates all of the "static" configuration of the cache build.
--
-- Static arguments are the ones that will not change during the execution of
-- the engine. They are a subset of the environment of the engine (see 'AppEnv'
-- and Note [Hasura Application State] for more information).
--
-- While 'AppEnv' has access to the union of *all* the static configuration of
-- the engine, more specific parts of the code should avoid relying directly on
-- it to avoid being tied to unrelated parts of the codebase. (See FIXME).
data CacheStaticConfig = CacheStaticConfig
  { CacheStaticConfig -> MaintenanceMode ()
_cscMaintenanceMode :: MaintenanceMode (),
    CacheStaticConfig -> EventingMode
_cscEventingMode :: EventingMode,
    CacheStaticConfig -> ReadOnlyMode
_cscReadOnlyMode :: ReadOnlyMode,
    CacheStaticConfig -> Logger Hasura
_cscLogger :: Logger Hasura,
    -- | Native queries can be enabled or disabled on the fly via a feature
    -- flag, however we only recognise a change on a restart
    CacheStaticConfig -> BackendType -> Bool
_cscAreNativeQueriesEnabled :: BackendType -> Bool,
    -- | Stored procedures can be enabled or disabled on the fly via a feature
    -- flag, however we only recognise a change on a restart
    CacheStaticConfig -> Bool
_cscAreStoredProceduresEnabled :: Bool
  }

class (Monad m) => HasCacheStaticConfig m where
  askCacheStaticConfig :: m CacheStaticConfig

instance (HasCacheStaticConfig m) => HasCacheStaticConfig (ReaderT r m) where
  askCacheStaticConfig :: ReaderT r m CacheStaticConfig
askCacheStaticConfig = m CacheStaticConfig -> ReaderT r m CacheStaticConfig
forall (m :: * -> *) a. Monad m => m a -> ReaderT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m CacheStaticConfig
forall (m :: * -> *). HasCacheStaticConfig m => m CacheStaticConfig
askCacheStaticConfig

instance (HasCacheStaticConfig m) => HasCacheStaticConfig (ExceptT e m) where
  askCacheStaticConfig :: ExceptT e m CacheStaticConfig
askCacheStaticConfig = m CacheStaticConfig -> ExceptT e m CacheStaticConfig
forall (m :: * -> *) a. Monad m => m a -> ExceptT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m CacheStaticConfig
forall (m :: * -> *). HasCacheStaticConfig m => m CacheStaticConfig
askCacheStaticConfig

instance (HasCacheStaticConfig m) => HasCacheStaticConfig (StateT s m) where
  askCacheStaticConfig :: StateT s m CacheStaticConfig
askCacheStaticConfig = m CacheStaticConfig -> StateT s m CacheStaticConfig
forall (m :: * -> *) a. Monad m => m a -> StateT s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m CacheStaticConfig
forall (m :: * -> *). HasCacheStaticConfig m => m CacheStaticConfig
askCacheStaticConfig

--------------------------------------------------------------------------------
-- dynamic config

-- | This type aggregates all of the "dynamic" configuration of the cache build.
--
-- Dynamic arguments are the ones that might change during the execution of the
-- engine. They are a subset of the 'AppContext' (see
-- Note [Hasura Application State] for more information).
--
-- While 'AppContext' has access to the union of *all* the dynamic configuration
-- of the engine, more specific parts of the code should avoid relying directly
-- on it to avoid being tied to unrelated parts of the codebase. (See FIXME).
data CacheDynamicConfig = CacheDynamicConfig
  { CacheDynamicConfig -> InferFunctionPermissions
_cdcFunctionPermsCtx :: Options.InferFunctionPermissions,
    CacheDynamicConfig -> RemoteSchemaPermissions
_cdcRemoteSchemaPermsCtx :: Options.RemoteSchemaPermissions,
    CacheDynamicConfig -> SQLGenCtx
_cdcSQLGenCtx :: SQLGenCtx,
    CacheDynamicConfig -> HashSet ExperimentalFeature
_cdcExperimentalFeatures :: HashSet ExperimentalFeature,
    CacheDynamicConfig -> NamingCase
_cdcDefaultNamingConvention :: NamingCase,
    CacheDynamicConfig -> MetadataDefaults
_cdcMetadataDefaults :: MetadataDefaults,
    CacheDynamicConfig -> ApolloFederationStatus
_cdcApolloFederationStatus :: ApolloFederationStatus,
    CacheDynamicConfig -> CloseWebsocketsOnMetadataChangeStatus
_cdcCloseWebsocketsOnMetadataChangeStatus :: CloseWebsocketsOnMetadataChangeStatus
  }
  deriving (CacheDynamicConfig -> CacheDynamicConfig -> Bool
(CacheDynamicConfig -> CacheDynamicConfig -> Bool)
-> (CacheDynamicConfig -> CacheDynamicConfig -> Bool)
-> Eq CacheDynamicConfig
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CacheDynamicConfig -> CacheDynamicConfig -> Bool
== :: CacheDynamicConfig -> CacheDynamicConfig -> Bool
$c/= :: CacheDynamicConfig -> CacheDynamicConfig -> Bool
/= :: CacheDynamicConfig -> CacheDynamicConfig -> Bool
Eq)