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

module Scion.PersistentBrowser.DbTypes where

import Scion.PersistentBrowser.Types

import Database.Persist.Sqlite
import Database.Persist.TH
import Data.Conduit (ResourceT)
import Control.Monad.Logger (LoggingT(..)) -- ,runStderrLoggingT
import Control.Monad.IO.Class (MonadIO)

type SQL a= SqlPersistT (LoggingT (ResourceT IO)) a

-- | wrapper around logging methods, so we can enable logging when we debug
runLogging :: MonadIO m => LoggingT m a -> m a
runLogging (LoggingT f) = f $ \_ _ _ _ -> return ()
        -- runStderrLoggingT

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistUpperCase|
    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]