module Hasql.Generate.Config
    ( Config (..)
    ) where

----------------------------------------------------------------------------------------------------

import           Data.Bool                 ( Bool (..) )
import           Data.Default.Class        ( Default (..) )
import           Data.String               ( String )

import           Hasql.Generate.Connection ( ConnectionInfo, libpqDefaults )

import           Language.Haskell.TH       ( Name )

----------------------------------------------------------------------------------------------------

{-  Top-level configuration for compile-time code generation.

    Embeds the database connection parameters ('ConnectionInfo') together with
    behavioral flags that control the shape of the generated code.

    Use @def@ for the default configuration, which reads connection details
    from standard PostgreSQL environment variables, sets False to allowDuplicateRecordFields
    and newtypePrimaryKeys, and has an emply override list.
-}
data Config
    = Config
      { Config -> ConnectionInfo
connection                 :: ConnectionInfo
      , Config -> Bool
allowDuplicateRecordFields :: Bool
      , Config -> Bool
newtypePrimaryKeys         :: Bool
      , Config -> [(String, Name)]
globalOverrides            :: [(String, Name)]
      }

{-  Default configuration:

    * Connection via libpq environment variables (@PGHOST@, @PGPORT@, etc.)
    * @allowDuplicateRecordFields = False@ — field names are prefixed with the
      type name so the generated code compiles without @DuplicateRecordFields@
    * @newtypePrimaryKeys = False@ — when @True@, generates newtype (single PK)
      or data record (composite PK) wrappers for primary keys, plus @HasPrimaryKey@
      instances for all tables with PKs
    * @globalOverrides = []@ — PG type overrides applied to all tables. Per-table
      'withOverrides' take precedence over global ones.
-}
instance Default Config where
  def :: Config
  def :: Config
def =
    Config
      { connection :: ConnectionInfo
connection = ConnectionInfo
libpqDefaults
      , allowDuplicateRecordFields :: Bool
allowDuplicateRecordFields = Bool
False
      , newtypePrimaryKeys :: Bool
newtypePrimaryKeys = Bool
False
      , globalOverrides :: [(String, Name)]
globalOverrides = []
      }