module Foundation.Type
  ( UniWorX(..)
  , SMTPPool
  , _appSettings', _appStatic, _appConnPool, _appSmtpPool, _appLdapPool, _appWidgetMemcached, _appHttpManager, _appLogger, _appLogSettings, _appCryptoIDKey, _appClusterID, _appInstanceID, _appJobState, _appSessionKey, _appSecretBoxKey, _appJSONWebKeySet, _appHealthReport
  ) where

import Import.NoFoundation
import Database.Persist.Sql (ConnectionPool)

import qualified Web.ClientSession as ClientSession

import Jobs.Types

import Yesod.Core.Types (Logger)

import Data.Set (Set)

import qualified Crypto.Saltine.Core.SecretBox as SecretBox
import qualified Jose.Jwk as Jose

import qualified Database.Memcached.Binary.IO as Memcached


type SMTPPool = Pool SMTPConnection

-- | The foundation datatype for your application. This can be a good place to
-- keep settings and values requiring initialization before your application
-- starts running, such as database connections. Every handler will have
-- access to the data present here.
data UniWorX = UniWorX
    { appSettings'       :: AppSettings
    , appStatic          :: EmbeddedStatic -- ^ Settings for static file serving.
    , appConnPool        :: ConnectionPool -- ^ Database connection pool.
    , appSmtpPool        :: Maybe SMTPPool
    , appLdapPool        :: Maybe LdapPool
    , appWidgetMemcached :: Maybe Memcached.Connection -- ^ Actually a proper pool
    , appHttpManager     :: Manager
    , appLogger          :: (ReleaseKey, TVar Logger)
    , appLogSettings     :: TVar LogSettings
    , appCryptoIDKey     :: CryptoIDKey
    , appClusterID       :: ClusterId
    , appInstanceID      :: InstanceId
    , appJobState        :: TMVar JobState
    , appSessionKey      :: ClientSession.Key
    , appSecretBoxKey    :: SecretBox.Key
    , appJSONWebKeySet   :: Jose.JwkSet
    , appHealthReport    :: TVar (Set (UTCTime, HealthReport))
    }

makeLenses_ ''UniWorX
instance HasInstanceID UniWorX InstanceId where
  instanceID = _appInstanceID
instance HasJSONWebKeySet UniWorX Jose.JwkSet where
  jsonWebKeySet = _appJSONWebKeySet
instance HasHttpManager UniWorX Manager where
  httpManager = _appHttpManager
instance HasAppSettings UniWorX where
  appSettings = _appSettings'
