{-# LANGUAGE OverloadedStrings #-}
module Main where

import           Data.Text.Lazy                       (toStrict)
import qualified Database.SQLite.Simple               as Db
import qualified Network.Wai.Middleware.RequestLogger as RequestLogger
import qualified Network.Wai.Middleware.Static        as Static
import           Text.Blaze.Renderer.Text
import           Web.Spock.Safe                       ((<//>))
import qualified Web.Spock.Safe                       as S
import qualified Within.DBMemorial.Post               as Post
import qualified Within.DBMemorial.User               as User
import qualified Within.DBMemorial.Views.Materialize  as Materialize

main :: IO ()
main = do
    conn <- Db.open "../db/posts.db"

    -- Simple tests to prove we're reading from SQLite
    r <- Db.query_ conn "SELECT * FROM Posts WHERE page=1" :: IO [Post.Post]
    let h = head r
    users <- Db.query_ conn "SELECT * FROM Users WHERE oid='51315c97a4c72da155001b9a' LIMIT 1" :: IO [User.User]
    let user = head users

    -- Set up the URL router
    S.runSpock 5000 $ S.spockT id $ do
         S.middleware $ Static.staticPolicy $ Static.addBase "public"
         S.middleware $ RequestLogger.logStdout

         S.get S.root $
           S.html $ toStrict (renderMarkup (Materialize.base "The OP" (Post.render h (User.render user))))

         S.get "user" $
           S.html $ toStrict (renderMarkup (Materialize.base "The first poster" (User.render user)))
