-- |

{-# LANGUAGE TemplateHaskell     #-}

module Gargantext.API.Admin.EnvTypes where

import Control.Lens
import Data.Pool (Pool)
import Database.PostgreSQL.Simple (Connection)
import GHC.Generics (Generic)
import Network.HTTP.Client (Manager)
import Servant.Client (BaseUrl)
import Servant.Job.Async (HasJobEnv(..), Job)
import System.Log.FastLogger
import qualified Servant.Job.Core

import Gargantext.API.Admin.Types
import Gargantext.API.Admin.Orchestrator.Types
import Gargantext.Core.NodeStory
import Gargantext.Core.Mail.Types (HasMail, mailSettings)
import Gargantext.Database.Prelude (HasConnectionPool(..), HasConfig(..))
import Gargantext.Prelude
import Gargantext.Prelude.Config (GargConfig(..))
import Gargantext.Prelude.Mail.Types (MailConfig)

data Env = Env
  { Env -> Settings
_env_settings  :: !Settings
  , Env -> LoggerSet
_env_logger    :: !LoggerSet
  , Env -> Pool Connection
_env_pool      :: !(Pool Connection)
  , Env -> NodeStoryEnv
_env_nodeStory :: !NodeStoryEnv
  , Env -> Manager
_env_manager   :: !Manager
  , Env -> BaseUrl
_env_self_url  :: !BaseUrl
  , Env -> ScrapersEnv
_env_scrapers  :: !ScrapersEnv
  , Env -> GargConfig
_env_config    :: !GargConfig
  , Env -> MailConfig
_env_mail      :: !MailConfig
  }
  deriving ((forall x. Env -> Rep Env x)
-> (forall x. Rep Env x -> Env) -> Generic Env
forall x. Rep Env x -> Env
forall x. Env -> Rep Env x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Env x -> Env
$cfrom :: forall x. Env -> Rep Env x
Generic)

makeLenses ''Env

instance HasConfig Env where
  hasConfig :: (GargConfig -> f GargConfig) -> Env -> f Env
hasConfig = (GargConfig -> f GargConfig) -> Env -> f Env
Lens' Env GargConfig
env_config

instance HasConnectionPool Env where
  connPool :: (Pool Connection -> f (Pool Connection)) -> Env -> f Env
connPool = (Pool Connection -> f (Pool Connection)) -> Env -> f Env
Lens' Env (Pool Connection)
env_pool

instance HasNodeStoryEnv Env where
  hasNodeStory :: (NodeStoryEnv -> f NodeStoryEnv) -> Env -> f Env
hasNodeStory = (NodeStoryEnv -> f NodeStoryEnv) -> Env -> f Env
Lens' Env NodeStoryEnv
env_nodeStory

instance HasNodeStoryVar Env where
  hasNodeStoryVar :: (([NodeId] -> IO (MVar NodeListStory))
 -> f ([NodeId] -> IO (MVar NodeListStory)))
-> Env -> f Env
hasNodeStoryVar = (NodeStoryEnv -> f NodeStoryEnv) -> Env -> f Env
forall env. HasNodeStoryEnv env => Getter env NodeStoryEnv
hasNodeStory ((NodeStoryEnv -> f NodeStoryEnv) -> Env -> f Env)
-> ((([NodeId] -> IO (MVar NodeListStory))
     -> f ([NodeId] -> IO (MVar NodeListStory)))
    -> NodeStoryEnv -> f NodeStoryEnv)
-> (([NodeId] -> IO (MVar NodeListStory))
    -> f ([NodeId] -> IO (MVar NodeListStory)))
-> Env
-> f Env
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (([NodeId] -> IO (MVar NodeListStory))
 -> f ([NodeId] -> IO (MVar NodeListStory)))
-> NodeStoryEnv -> f NodeStoryEnv
Lens' NodeStoryEnv ([NodeId] -> IO (MVar NodeListStory))
nse_getter

instance HasNodeStorySaver Env where
  hasNodeStorySaver :: (IO () -> f (IO ())) -> Env -> f Env
hasNodeStorySaver = (NodeStoryEnv -> f NodeStoryEnv) -> Env -> f Env
forall env. HasNodeStoryEnv env => Getter env NodeStoryEnv
hasNodeStory ((NodeStoryEnv -> f NodeStoryEnv) -> Env -> f Env)
-> ((IO () -> f (IO ())) -> NodeStoryEnv -> f NodeStoryEnv)
-> (IO () -> f (IO ()))
-> Env
-> f Env
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (IO () -> f (IO ())) -> NodeStoryEnv -> f NodeStoryEnv
Lens' NodeStoryEnv (IO ())
nse_saver

instance HasSettings Env where
  settings :: (Settings -> f Settings) -> Env -> f Env
settings = (Settings -> f Settings) -> Env -> f Env
Lens' Env Settings
env_settings

instance HasMail Env where
  mailSettings :: (MailConfig -> f MailConfig) -> Env -> f Env
mailSettings = (MailConfig -> f MailConfig) -> Env -> f Env
Lens' Env MailConfig
env_mail


instance Servant.Job.Core.HasEnv Env (Job JobLog JobLog) where
  _env :: (Env (Job JobLog JobLog) -> f (Env (Job JobLog JobLog)))
-> Env -> f Env
_env = (ScrapersEnv -> f ScrapersEnv) -> Env -> f Env
Lens' Env ScrapersEnv
env_scrapers ((ScrapersEnv -> f ScrapersEnv) -> Env -> f Env)
-> ((Env (Job JobLog JobLog) -> f (Env (Job JobLog JobLog)))
    -> ScrapersEnv -> f ScrapersEnv)
-> (Env (Job JobLog JobLog) -> f (Env (Job JobLog JobLog)))
-> Env
-> f Env
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Env (Job JobLog JobLog) -> f (Env (Job JobLog JobLog)))
-> ScrapersEnv -> f ScrapersEnv
forall env a. HasEnv env a => Lens' env (Env a)
Servant.Job.Core._env

instance HasJobEnv Env JobLog JobLog where
  job_env :: (ScrapersEnv -> f ScrapersEnv) -> Env -> f Env
job_env = (ScrapersEnv -> f ScrapersEnv) -> Env -> f Env
Lens' Env ScrapersEnv
env_scrapers

data MockEnv = MockEnv
  { MockEnv -> FireWall
_menv_firewall :: !FireWall
  }
  deriving ((forall x. MockEnv -> Rep MockEnv x)
-> (forall x. Rep MockEnv x -> MockEnv) -> Generic MockEnv
forall x. Rep MockEnv x -> MockEnv
forall x. MockEnv -> Rep MockEnv x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep MockEnv x -> MockEnv
$cfrom :: forall x. MockEnv -> Rep MockEnv x
Generic)

makeLenses ''MockEnv



data DevEnv = DevEnv
  { DevEnv -> Settings
_dev_env_settings  :: !Settings
  , DevEnv -> GargConfig
_dev_env_config    :: !GargConfig
  , DevEnv -> Pool Connection
_dev_env_pool      :: !(Pool Connection)
  , DevEnv -> NodeStoryEnv
_dev_env_nodeStory :: !NodeStoryEnv
  , DevEnv -> MailConfig
_dev_env_mail      :: !MailConfig
  }

makeLenses ''DevEnv

instance HasConfig DevEnv where
  hasConfig :: (GargConfig -> f GargConfig) -> DevEnv -> f DevEnv
hasConfig = (GargConfig -> f GargConfig) -> DevEnv -> f DevEnv
Lens' DevEnv GargConfig
dev_env_config

instance HasConnectionPool DevEnv where
  connPool :: (Pool Connection -> f (Pool Connection)) -> DevEnv -> f DevEnv
connPool = (Pool Connection -> f (Pool Connection)) -> DevEnv -> f DevEnv
Lens' DevEnv (Pool Connection)
dev_env_pool

instance HasSettings DevEnv where
  settings :: (Settings -> f Settings) -> DevEnv -> f DevEnv
settings = (Settings -> f Settings) -> DevEnv -> f DevEnv
Lens' DevEnv Settings
dev_env_settings


instance HasNodeStoryEnv DevEnv where
  hasNodeStory :: (NodeStoryEnv -> f NodeStoryEnv) -> DevEnv -> f DevEnv
hasNodeStory = (NodeStoryEnv -> f NodeStoryEnv) -> DevEnv -> f DevEnv
Lens' DevEnv NodeStoryEnv
dev_env_nodeStory

instance HasNodeStoryVar DevEnv where
  hasNodeStoryVar :: (([NodeId] -> IO (MVar NodeListStory))
 -> f ([NodeId] -> IO (MVar NodeListStory)))
-> DevEnv -> f DevEnv
hasNodeStoryVar = (NodeStoryEnv -> f NodeStoryEnv) -> DevEnv -> f DevEnv
forall env. HasNodeStoryEnv env => Getter env NodeStoryEnv
hasNodeStory ((NodeStoryEnv -> f NodeStoryEnv) -> DevEnv -> f DevEnv)
-> ((([NodeId] -> IO (MVar NodeListStory))
     -> f ([NodeId] -> IO (MVar NodeListStory)))
    -> NodeStoryEnv -> f NodeStoryEnv)
-> (([NodeId] -> IO (MVar NodeListStory))
    -> f ([NodeId] -> IO (MVar NodeListStory)))
-> DevEnv
-> f DevEnv
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (([NodeId] -> IO (MVar NodeListStory))
 -> f ([NodeId] -> IO (MVar NodeListStory)))
-> NodeStoryEnv -> f NodeStoryEnv
Lens' NodeStoryEnv ([NodeId] -> IO (MVar NodeListStory))
nse_getter

instance HasNodeStorySaver DevEnv where
  hasNodeStorySaver :: (IO () -> f (IO ())) -> DevEnv -> f DevEnv
hasNodeStorySaver = (NodeStoryEnv -> f NodeStoryEnv) -> DevEnv -> f DevEnv
forall env. HasNodeStoryEnv env => Getter env NodeStoryEnv
hasNodeStory ((NodeStoryEnv -> f NodeStoryEnv) -> DevEnv -> f DevEnv)
-> ((IO () -> f (IO ())) -> NodeStoryEnv -> f NodeStoryEnv)
-> (IO () -> f (IO ()))
-> DevEnv
-> f DevEnv
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (IO () -> f (IO ())) -> NodeStoryEnv -> f NodeStoryEnv
Lens' NodeStoryEnv (IO ())
nse_saver

instance HasMail DevEnv where
  mailSettings :: (MailConfig -> f MailConfig) -> DevEnv -> f DevEnv
mailSettings = (MailConfig -> f MailConfig) -> DevEnv -> f DevEnv
Lens' DevEnv MailConfig
dev_env_mail