{-|
Description: Test module to be sure the generic 'Session' is sane
-}
module TsWeb.Tables.Session.Test where

import TsWeb.Session
import TsWeb.Tables.Session

import Data.Pool (Pool)
import Database.Beam
import TsWeb.Types.Db (ReadOnlyConn, ReadWriteConn)
import Web.Spock.Config
import Web.Spock.Internal.SessionManager as SM (Session(..))

-- | Some sample user info
data UsersMixin f = Address
  { loginid :: C f Int
  , masqid :: C f Int
  , remember :: C f Bool
  } deriving (Generic)

-- | Concrete type for users
type Users = UsersMixin Identity

instance Beamable UsersMixin
instance UserData Users where
  rememberMe = remember

-- | Spock session alias to use our User
type Sess conn st = SM.Session conn Users st

-- | Beam database definition to hold our session
data Db f = Db
  { session :: f (TableEntity (SessionT UsersMixin))
  } deriving (Generic)

instance Database be Db

-- | Beam database instance
db :: DatabaseSettings be Db
db = defaultDbSettings

-- | Sample of patch to be sure that all the types work enough to make a spock
-- config wrapper
patch ::
     Pool ReadOnlyConn
  -> Pool ReadWriteConn
  -> SpockCfg conn Users st
  -> SpockCfg conn Users st
patch = patchConfig (session db)