module Data.BBS where

import Data.Time.Clock.POSIX (getPOSIXTime)

data Post = Post { pName    :: String
                 , pContent :: String
                 , pTime    :: Int }
          deriving (Read, Show)

data Thread = Thread { tName    :: String
                     , tSubject :: String
                     , tContent :: String
                     , tTime    :: Int
                     , tPosts   :: [Post] }
            deriving (Read, Show)

-- Creates a post from two strings - name and content.
createPost :: String -> String -> IO Post
createPost n c = do
  t <- round <$> getPOSIXTime
  return Post { pName    = n
              , pContent = c
              , pTime    = t }

-- Creates a thread from three strings - name, subject, content.
createThread :: String -> String -> String -> IO Thread
createThread n s c = do
  t <- round <$> getPOSIXTime
  return Thread { tName    = n
                , tSubject = s
                , tContent = c
                , tTime    = t
                , tPosts   = [] }

-- Adds a post to a thread.
appendPost :: Thread -> Post -> Thread
appendPost t p = t { tPosts = reverse $ p : existing }
  where existing = tPosts t
