{-|
Module      : Gargantext.Database
Description : Tools for Database
Copyright   : (c) CNRS, 2017-Present
License     : AGPL + CECILL v3
Maintainer  : team@gargantext.org
Stability   : experimental
Portability : POSIX

Target: just import this module and nothing else to work with
Gargantext's database.

TODO: configure nodes table in Haskell (Config typenames etc.)
-}


{-# OPTIONS_GHC -fno-warn-orphans        #-}

module Gargantext.Database.Admin.Config
    where

import Control.Lens     (view)
import Data.List        (lookup)
import Data.Maybe       (fromMaybe)
import Data.Text        (Text,pack)
import Data.Tuple.Extra (swap)
import Gargantext.Core (HasDBid(..))
import Gargantext.Database.Admin.Types.Node
import Gargantext.Database.Schema.Node
import Gargantext.Prelude

-- TODO put this in config.ini file
corpusMasterName :: Text
corpusMasterName :: Text
corpusMasterName = Text
"Main"

userMaster :: Text
userMaster :: Text
userMaster = Text
"gargantua"

userArbitrary :: Text
userArbitrary :: Text
userArbitrary = Text
"user1"

instance HasDBid NodeType where
  toDBid :: NodeType -> Int
toDBid  = NodeType -> Int
nodeTypeId
  fromDBid :: Int -> NodeType
fromDBid = Int -> NodeType
fromNodeTypeId


nodeTypeId :: NodeType -> NodeTypeId
nodeTypeId :: NodeType -> Int
nodeTypeId NodeType
n =
  case NodeType
n of
    NodeType
NodeUser          -> Int
1
    NodeType
NodeFolder        -> Int
2
    NodeType
NodeFolderPrivate -> Int
20
    NodeType
NodeFolderShared  -> Int
21
    NodeType
NodeTeam          -> Int
210
    NodeType
NodeFolderPublic  -> Int
22
    NodeType
NodeCorpusV3      -> Int
3
    NodeType
NodeCorpus        -> Int
30
    NodeType
NodeAnnuaire      -> Int
31
    NodeType
NodeTexts         -> Int
40
    NodeType
NodeDocument      -> Int
4
    NodeType
NodeContact       -> Int
41
  --NodeSwap   -> 19

----  Lists
    NodeType
NodeList      -> Int
5
    NodeType
NodeListCooc  -> Int
50
    NodeType
NodeModel -> Int
52

----  Scores
--    NodeOccurrences -> 10
    NodeType
NodeGraph       -> Int
9
    NodeType
NodePhylo       -> Int
90
--    NodeChart       -> 7
    NodeType
NodeDashboard   -> Int
71
--    NodeNoteBook    -> 88

    NodeType
NodeFile        -> Int
101

    NodeType
NodeFrameWrite    -> Int
991
    NodeType
NodeFrameCalc     -> Int
992
    NodeType
NodeFrameNotebook -> Int
993
    NodeType
NodeFrameVisio    -> Int
994

--  Cooccurrences -> 9
--
--  Specclusion  -> 11
--  Genclusion   -> 18
--  Cvalue       -> 12
--
--  TfidfCorpus  -> 13
--  TfidfGlobal  -> 14
--
--  TirankLocal  -> 16
--  TirankGlobal -> 17

--  Node management
--  NodeFavorites    -> 15

hasNodeType :: forall a. Node a -> NodeType -> Bool
hasNodeType :: Node a -> NodeType -> Bool
hasNodeType Node a
n NodeType
nt = (Getting Int (Node a) Int -> Node a -> Int
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting Int (Node a) Int
forall id hash_id typename1 user_id parent_id name date hyperdata
       typename2.
Lens
  (NodePoly
     id hash_id typename1 user_id parent_id name date hyperdata)
  (NodePoly
     id hash_id typename2 user_id parent_id name date hyperdata)
  typename1
  typename2
node_typename Node a
n) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== (NodeType -> Int
forall a. HasDBid a => a -> Int
toDBid NodeType
nt)

isInNodeTypes :: forall a. Node a -> [NodeType] -> Bool
isInNodeTypes :: Node a -> [NodeType] -> Bool
isInNodeTypes Node a
n [NodeType]
ts = Int -> [Int] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem (Getting Int (Node a) Int -> Node a -> Int
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting Int (Node a) Int
forall id hash_id typename1 user_id parent_id name date hyperdata
       typename2.
Lens
  (NodePoly
     id hash_id typename1 user_id parent_id name date hyperdata)
  (NodePoly
     id hash_id typename2 user_id parent_id name date hyperdata)
  typename1
  typename2
node_typename Node a
n) ((NodeType -> Int) -> [NodeType] -> [Int]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map NodeType -> Int
forall a. HasDBid a => a -> Int
toDBid [NodeType]
ts)

-- | Nodes are typed in the database according to a specific ID
--
nodeTypeInv :: [(NodeTypeId, NodeType)]
nodeTypeInv :: [(Int, NodeType)]
nodeTypeInv = ((NodeType, Int) -> (Int, NodeType))
-> [(NodeType, Int)] -> [(Int, NodeType)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map (NodeType, Int) -> (Int, NodeType)
forall a b. (a, b) -> (b, a)
swap [(NodeType, Int)]
nodeTypes

nodeTypes :: [(NodeType, NodeTypeId)]
nodeTypes :: [(NodeType, Int)]
nodeTypes = [ (NodeType
n, NodeType -> Int
forall a. HasDBid a => a -> Int
toDBid NodeType
n) | NodeType
n <- [NodeType]
allNodeTypes ]

fromNodeTypeId :: NodeTypeId -> NodeType
fromNodeTypeId :: Int -> NodeType
fromNodeTypeId Int
tId = NodeType -> Maybe NodeType -> NodeType
forall a. a -> Maybe a -> a
fromMaybe (Text -> NodeType
forall a. HasCallStack => Text -> a
panic (Text -> NodeType) -> Text -> NodeType
forall a b. (a -> b) -> a -> b
$ String -> Text
pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ String
"Type Id " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
tId String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" does not exist")
                               (Int -> [(Int, NodeType)] -> Maybe NodeType
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Int
tId [(Int, NodeType)]
nodeTypeInv)