{-# LANGUAGE QuasiQuotes, TemplateHaskell, TypeFamilies, OverloadedStrings, GADTs, FlexibleContexts, EmptyDataDecls #-}

module Scion.PersistentBrowser.DbTypes where

import Database.Persist
-- import Database.Persist.Base
import Database.Persist.Sqlite
import Database.Persist.TH

data DbDeclType = DbData | DbNewType | DbClass | DbInstance | DbSignature | DbType
    deriving (Show, Read, Eq)
derivePersistField "DbDeclType"

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persist|
    name      String
    version   String
    doc       String Maybe
    UniqueVersion name version
    name      String
    doc       String Maybe
    packageId DbPackageId
    declType  DbDeclType
    name      String
    doc       String Maybe
    -- Depending on the type of decl,
    -- it will have some of these
    kind      String Maybe
    signature String Maybe
    equals    String Maybe
    moduleId  DbModuleId
    name      String
    declId    DbDeclId
    name      String
    declId    DbDeclId
    shown     String
    declId    DbDeclId
    name      String
    -- Called 'type' in Json output
    signature String
    declId    DbDeclId

-- |Information needed to search a package.
data DbPackageIdentifier = DbPackageIdentifier String String -- name, version
     deriving Eq

dbPackageToIdentifier :: DbPackage -> DbPackageIdentifier
dbPackageToIdentifier (DbPackage name version _) = DbPackageIdentifier name version

-- |Complete information for a declaration.
--  Look at its ToJSON instance to know which one is used in each kind of declaration.
data DbCompleteDecl = DbCompleteDecl DbDecl [DbContext] [DbTyVar] [DbFunDep] [DbConstructor]