module Model.Migration.Types where 

import ClassyPrelude.Yesod
import Data.Aeson
import Data.Aeson.TH (deriveJSON, defaultOptions)

import Utils.PathPiece

import qualified Model as Current
import qualified Model.Types.JSON as Current

import Data.Universe


data SheetType
  = Bonus  { maxPoints :: Current.Points } -- Erhöht nicht das Maximum, wird gutgeschrieben
  | Normal { maxPoints :: Current.Points } -- Erhöht das Maximum, wird gutgeschrieben
  | Pass   { maxPoints, passingPoints :: Current.Points }
  | NotGraded
  deriving (Show, Read, Eq)

sheetType :: SheetType -> Current.SheetType
sheetType Bonus {..}  = Current.Bonus  Current.Points {..}
sheetType Normal {..} = Current.Normal Current.Points {..}
sheetType Pass {..}   = Current.Normal Current.PassPoints {..}
sheetType NotGraded   = Current.NotGraded


data UploadMode = NoUpload | Upload { unpackZips :: Bool }
  deriving (Show, Read, Eq, Ord, Generic)

deriveJSON defaultOptions ''UploadMode
Current.derivePersistFieldJSON ''UploadMode

instance Universe UploadMode where
  universe = NoUpload : (Upload <$> universe)
instance Finite UploadMode

instance PathPiece UploadMode where
  toPathPiece = \case
    NoUpload     -> "no-upload"
    Upload True  -> "unpack"
    Upload False -> "no-unpack"
  fromPathPiece = finiteFromPathPiece

data SheetSubmissionMode = NoSubmissions
                         | CorrectorSubmissions
                         | UserSubmissions
  deriving (Show, Read, Eq, Ord, Enum, Bounded, Generic)

deriveJSON defaultOptions
  { constructorTagModifier = camelToPathPiece
  } ''SheetSubmissionMode
derivePersistField "SheetSubmissionMode"

instance Universe SheetSubmissionMode
instance Finite SheetSubmissionMode

nullaryPathPiece ''SheetSubmissionMode camelToPathPiece

  
{- TODO:
  * RenderMessage instance for newtype(SheetType) if needed
-}


deriveJSON defaultOptions ''SheetType
Current.derivePersistFieldJSON ''SheetType
