{-# LANGUAGE UndecidableInstances #-}

module Hasura.LogicalModel.Cache
  ( LogicalModelInfo (..),
    LogicalModelCache,
  )
where

import Data.Aeson (ToJSON (..), genericToJSON)
import Data.HashMap.Strict.InsOrd qualified as InsOrdHashMap
import Hasura.LogicalModel.Types (LogicalModelField, LogicalModelName)
import Hasura.Prelude hiding (first)
import Hasura.RQL.Types.Backend (Backend (..))
import Hasura.RQL.Types.BackendType (BackendType)
import Hasura.Table.Cache (RolePermInfoMap)

type LogicalModelCache b = HashMap LogicalModelName (LogicalModelInfo b)

-- | Description of a logical model for use in metadata (after schema cache)
data LogicalModelInfo (b :: BackendType) = LogicalModelInfo
  { forall (b :: BackendType). LogicalModelInfo b -> LogicalModelName
_lmiName :: LogicalModelName,
    forall (b :: BackendType).
LogicalModelInfo b
-> InsOrdHashMap (Column b) (LogicalModelField b)
_lmiFields :: InsOrdHashMap.InsOrdHashMap (Column b) (LogicalModelField b),
    forall (b :: BackendType). LogicalModelInfo b -> Maybe Text
_lmiDescription :: Maybe Text,
    forall (b :: BackendType). LogicalModelInfo b -> RolePermInfoMap b
_lmiPermissions :: RolePermInfoMap b
  }
  deriving ((forall x. LogicalModelInfo b -> Rep (LogicalModelInfo b) x)
-> (forall x. Rep (LogicalModelInfo b) x -> LogicalModelInfo b)
-> Generic (LogicalModelInfo b)
forall x. Rep (LogicalModelInfo b) x -> LogicalModelInfo b
forall x. LogicalModelInfo b -> Rep (LogicalModelInfo b) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (b :: BackendType) x.
Rep (LogicalModelInfo b) x -> LogicalModelInfo b
forall (b :: BackendType) x.
LogicalModelInfo b -> Rep (LogicalModelInfo b) x
$cfrom :: forall (b :: BackendType) x.
LogicalModelInfo b -> Rep (LogicalModelInfo b) x
from :: forall x. LogicalModelInfo b -> Rep (LogicalModelInfo b) x
$cto :: forall (b :: BackendType) x.
Rep (LogicalModelInfo b) x -> LogicalModelInfo b
to :: forall x. Rep (LogicalModelInfo b) x -> LogicalModelInfo b
Generic)

instance
  (Backend b, ToJSON (RolePermInfoMap b)) =>
  ToJSON (LogicalModelInfo b)
  where
  toJSON :: LogicalModelInfo b -> Value
toJSON = Options -> LogicalModelInfo b -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON Options
hasuraJSON