module Job.Kind
  ( Kind(..)
  ) where

import Database.SQLite.Simple (SQLData(SQLText))
import Database.SQLite.Simple.FromField (fieldData, FromField(fromField))
import Database.SQLite.Simple.Ok (Ok(Ok, Errors))
import Database.SQLite.Simple.ToField (ToField(toField))
import qualified Data.Text as T

data Kind =
  MonthlyPayment
  | WeeklyReport
  deriving (Eq, Show, Read)

instance FromField Kind where
  fromField field = case fieldData field of
    SQLText text -> Ok (read (T.unpack text) :: Kind)
    _ -> Errors [error "SQLText field required for job kind"]

instance ToField Kind where
  toField kind = SQLText . T.pack . show $ kind