module IHP.Controller.RequestContext
( RequestContext (..)
, Respond
, getConfig
, RequestBody (..)
) where

import           ClassyPrelude
import qualified Data.ByteString.Lazy as LBS
import           Network.Wai                   (Request, Response, ResponseReceived)
import           Network.Wai.Parse (File, Param)
import qualified Data.Vault.Lazy               as Vault
import           Network.Wai.Session           (Session)
import           IHP.FrameworkConfig
import qualified Data.Aeson as Aeson


type Respond = Response -> IO ResponseReceived

data RequestBody
    = FormBody { RequestBody -> [Param]
params :: [Param], RequestBody -> [File ByteString]
files :: [File LBS.ByteString] }
    | JSONBody { RequestBody -> Maybe Value
jsonPayload :: Maybe Aeson.Value, RequestBody -> ByteString
rawPayload :: LByteString } -- ^ The jsonPayload is the decoded json request. We keep a copy of the original json request in rawPayload, so that you can e.g. get a HMAC signature from the request

data RequestContext = RequestContext
    { RequestContext -> Request
request :: Request
    , RequestContext -> Respond
respond :: Respond
    , RequestContext -> RequestBody
requestBody :: RequestBody
    , RequestContext -> Key (Session IO String String)
vault :: (Vault.Key (Session IO String String))
    , RequestContext -> FrameworkConfig
frameworkConfig :: FrameworkConfig
    }

instance ConfigProvider RequestContext where
    getFrameworkConfig :: RequestContext -> FrameworkConfig
getFrameworkConfig = RequestContext -> FrameworkConfig
frameworkConfig