-- | Globally Unique IDentifiers.
module Data.CQRS.GUID
       ( GUID
       , newGUID
       ) where

import           Control.Monad (liftM)
import           Data.ByteString (ByteString)
import qualified Data.ByteString as B
import           Data.ByteString.Char8 ()
import           Data.Default (Default(..))
import           Data.Serialize (Serialize(..))
import           Data.Typeable (Typeable)
import           Data.Word (Word8)
import           System.Random (randomRIO)

-- | A GUID for values of type 'a'.
newtype GUID a = GUID ByteString
              deriving (Show, Typeable, Eq)

-- | Default GUID value.
instance Default (GUID a) where
    def = GUID $ B.pack $ replicate 32 0

-- | Get a random byte.
randomWord8 :: IO Word8
randomWord8 = fmap fromInteger $ randomRIO (0,255)

-- | Create a new random GUID.
newGUID :: IO (GUID a)
newGUID = do
  uuid <- sequence $ replicate 32 randomWord8
  return $ GUID $ B.pack uuid

-- | Serialize instance.
instance Serialize (GUID a) where
    put (GUID s) = put s
    get = liftM GUID get