{-# options_haddock prune #-}
{-# options_ghc -Wno-partial-type-signatures #-}

-- | Description: Sqel Dd definitions for account and auth tables
module Polysemy.Account.Db.Dd where

import Prelude hiding (Mod)
import Sqel (
  Dd,
  EnumColumn,
  Mod,
  Mods,
  Prim,
  PrimNewtype,
  PrimaryKey,
  Prod,
  Sqel,
  Uid,
  UidDd,
  array,
  enum,
  nullable,
  pk,
  prim,
  primNewtype,
  prod,
  uid,
  type (*>),
  type (:>) ((:>)),
  type (>),
  )
import Sqel.Codec (PrimColumn)
import Sqel.Comp (Column)
import Sqel.Data.Codec (FullCodec)
import Sqel.Data.PgType (PgPrimName)
import Sqel.Data.TableSchema (TableSchema)
import Sqel.Ext (named)
import Sqel.PgType (tableSchema)
import Sqel.ReifyCodec (ReifyCodec)
import Sqel.ReifyDd (ReifyDd)

import Polysemy.Account.Data.Account (Account, AccountP)
import Polysemy.Account.Data.AccountAuth (AccountAuth)
import Polysemy.Account.Data.AccountName (AccountName)
import Polysemy.Account.Data.AccountStatus (AccountStatus)
import Polysemy.Account.Data.Privilege (Privilege)

type DdAccount i p s =
  UidDd (Mod PrimaryKey (Prim "id" i)) (
    Prod (Account p) *>
    PrimNewtype "name" AccountName >
    Mods [PgPrimName, EnumColumn] (Prim "status" AccountStatus) >
    s
  )

privileges :: Sqel [Privilege] _
privileges :: Sqel
  [Privilege]
  '( 'SelSymbol "privileges",
     '[ArrayColumn [], PgPrimName, EnumColumn], 'Prim)
privileges =
  forall (name :: Symbol) (s0 :: DdK).
Rename s0 (SetName s0 name) =>
Dd s0 -> Dd (SetName s0 name)
named @"privileges" (forall (f :: * -> *) a (p :: [*]) (sel :: Sel).
Dd ('DdK sel p a 'Prim)
-> Dd ('DdK sel (ArrayColumn f : p) (f a) 'Prim)
array forall a. Dd ('DdK 'SelAuto '[PgPrimName, EnumColumn] a 'Prim)
enum)

account ::
  Column p "privileges" s s =>
  Dd s ->
  Dd (DdAccount i p s)
account :: forall p (s :: DdK) i.
Column p "privileges" s s =>
Dd s -> Dd (DdAccount i p s)
account Dd s
p =
  forall i a (si :: DdK) (sa :: DdK) (s :: DdK).
UidColumn i a si sa s =>
Dd si -> Dd sa -> Dd s
uid (forall (s0 :: DdK) (s1 :: DdK).
AddMod PrimaryKey s0 s1 =>
Dd s0 -> Dd s1
pk forall a. Dd ('DdK 'SelAuto '[] a 'Prim)
prim) (forall a arg (s :: DdK). Product a arg s => arg -> Dd s
prod (forall a w (err :: ErrorMessage).
(err ~ NewtypeError, UnwrapNewtype err a w) =>
Dd ('DdK 'SelAuto '[Newtype a w] a 'Prim)
primNewtype forall a b. a -> b -> a :> b
:> forall a. Dd ('DdK 'SelAuto '[PgPrimName, EnumColumn] a 'Prim)
enum forall a b. a -> b -> a :> b
:> Dd s
p))

accountP :: Sqel (Uid i AccountP) _
accountP :: Sqel
  (Uid i AccountP)
  '( 'SelAuto, '[],
     'Comp
       ('TSel 'DefaultPrefix "Account")
       ('Prod 'Reg)
       'Nest
       '[ 'DdK ('SelSymbol "id") '[PrimaryKey] i 'Prim,
          'DdK
            'SelAuto
            '[]
            AccountP
            ('Comp
               ('TSel 'DefaultPrefix "Account")
               ('Prod 'Reg)
               'Merge
               '[ 'DdK
                    ('SelSymbol "name") '[Newtype AccountName Text] AccountName 'Prim,
                  'DdK
                    ('SelSymbol "status")
                    '[PgPrimName, EnumColumn]
                    AccountStatus
                    'Prim,
                  'DdK
                    ('SelSymbol "privileges")
                    '[ArrayColumn [], PgPrimName, EnumColumn]
                    [Privilege]
                    'Prim])])
accountP =
  forall p (s :: DdK) i.
Column p "privileges" s s =>
Dd s -> Dd (DdAccount i p s)
account Dd
  ('DdK
     ('SelSymbol "privileges")
     '[ArrayColumn [], PgPrimName, EnumColumn]
     [Privilege]
     'Prim)
privileges

accountSchema ::
  PrimColumn i =>
  Column p "privileges" s s =>
  ReifyCodec FullCodec s p =>
  ReifyDd s =>
  Dd s ->
  TableSchema (Uid i (Account p))
accountSchema :: forall i p (s :: DdK).
(PrimColumn i, Column p "privileges" s s, ReifyCodec FullCodec s p,
 ReifyDd s) =>
Dd s -> TableSchema (Uid i (Account p))
accountSchema Dd s
p =
  forall (table :: DdK).
MkTableSchema table =>
Dd table -> TableSchema (DdType table)
tableSchema (forall p (s :: DdK) i.
Column p "privileges" s s =>
Dd s -> Dd (DdAccount i p s)
account Dd s
p)

accountSchemaP ::
  PrimColumn i =>
  TableSchema (Uid i AccountP)
accountSchemaP :: forall i. PrimColumn i => TableSchema (Uid i AccountP)
accountSchemaP =
  forall (table :: DdK).
MkTableSchema table =>
Dd table -> TableSchema (DdType table)
tableSchema forall i.
Dd
  ('DdK
     'SelAuto
     '[]
     (Uid i AccountP)
     ('Comp
        ('TSel 'DefaultPrefix "Account")
        ('Prod 'Reg)
        'Nest
        '[ 'DdK ('SelSymbol "id") '[PrimaryKey] i 'Prim,
           'DdK
             'SelAuto
             '[]
             AccountP
             ('Comp
                ('TSel 'DefaultPrefix "Account")
                ('Prod 'Reg)
                'Merge
                '[ 'DdK
                     ('SelSymbol "name") '[Newtype AccountName Text] AccountName 'Prim,
                   'DdK
                     ('SelSymbol "status")
                     '[PgPrimName, EnumColumn]
                     AccountStatus
                     'Prim,
                   'DdK
                     ('SelSymbol "privileges")
                     '[ArrayColumn [], PgPrimName, EnumColumn]
                     [Privilege]
                     'Prim])]))
accountP

accountAuth ::
  Sqel (Uid i (AccountAuth i)) _
accountAuth :: Sqel
  (Uid i (AccountAuth i))
  '( 'SelAuto, '[],
     'Comp
       (DdTypeSel
          ('DdK
             'SelAuto
             '[]
             (AccountAuth i)
             ('Comp
                ('TSel 'DefaultPrefix "AccountAuth")
                ('Prod 'Reg)
                'Nest
                '[ 'DdK ('SelSymbol "account") '[] i 'Prim,
                   'DdK
                     ('SelSymbol "description")
                     '[Newtype AccountAuthDescription Text]
                     AccountAuthDescription
                     'Prim,
                   'DdK
                     ('SelSymbol "password")
                     '[Newtype HashedPassword Text]
                     HashedPassword
                     'Prim,
                   'DdK ('SelSymbol "expiry") '[Nullable] (Maybe Datetime) 'Prim])))
       ('Prod 'Reg)
       'Nest
       '[ 'DdK ('SelSymbol "id") '[PrimaryKey] i 'Prim,
          'DdK
            'SelAuto
            '[]
            (AccountAuth i)
            ('Comp
               ('TSel 'DefaultPrefix "AccountAuth")
               ('Prod 'Reg)
               'Merge
               '[ 'DdK ('SelSymbol "account") '[] i 'Prim,
                  'DdK
                    ('SelSymbol "description")
                    '[Newtype AccountAuthDescription Text]
                    AccountAuthDescription
                    'Prim,
                  'DdK
                    ('SelSymbol "password")
                    '[Newtype HashedPassword Text]
                    HashedPassword
                    'Prim,
                  'DdK ('SelSymbol "expiry") '[Nullable] (Maybe Datetime) 'Prim])])
accountAuth =
  forall i a (si :: DdK) (sa :: DdK) (s :: DdK).
UidColumn i a si sa s =>
Dd si -> Dd sa -> Dd s
uid (forall (s0 :: DdK) (s1 :: DdK).
AddMod PrimaryKey s0 s1 =>
Dd s0 -> Dd s1
pk forall a. Dd ('DdK 'SelAuto '[] a 'Prim)
prim) (forall a arg (s :: DdK). Product a arg s => arg -> Dd s
prod (forall a. Dd ('DdK 'SelAuto '[] a 'Prim)
prim forall a b. a -> b -> a :> b
:> forall a w (err :: ErrorMessage).
(err ~ NewtypeError, UnwrapNewtype err a w) =>
Dd ('DdK 'SelAuto '[Newtype a w] a 'Prim)
primNewtype forall a b. a -> b -> a :> b
:> forall a w (err :: ErrorMessage).
(err ~ NewtypeError, UnwrapNewtype err a w) =>
Dd ('DdK 'SelAuto '[Newtype a w] a 'Prim)
primNewtype forall a b. a -> b -> a :> b
:> forall (s0 :: DdK) (s1 :: DdK) (s2 :: DdK).
(AddMod Nullable s0 s1, MkNullable s1 s2) =>
Dd s0 -> Dd s2
nullable forall a. Dd ('DdK 'SelAuto '[] a 'Prim)
prim))

accountAuthSchema ::
  PrimColumn i =>
  TableSchema (Uid i (AccountAuth i))
accountAuthSchema :: forall i. PrimColumn i => TableSchema (Uid i (AccountAuth i))
accountAuthSchema =
  forall (table :: DdK).
MkTableSchema table =>
Dd table -> TableSchema (DdType table)
tableSchema forall i.
Dd
  ('DdK
     'SelAuto
     '[]
     (Uid i (AccountAuth i))
     ('Comp
        ('TSel 'DefaultPrefix "AccountAuth")
        ('Prod 'Reg)
        'Nest
        '[ 'DdK ('SelSymbol "id") '[PrimaryKey] i 'Prim,
           'DdK
             'SelAuto
             '[]
             (AccountAuth i)
             ('Comp
                ('TSel 'DefaultPrefix "AccountAuth")
                ('Prod 'Reg)
                'Merge
                '[ 'DdK ('SelSymbol "account") '[] i 'Prim,
                   'DdK
                     ('SelSymbol "description")
                     '[Newtype AccountAuthDescription Text]
                     AccountAuthDescription
                     'Prim,
                   'DdK
                     ('SelSymbol "password")
                     '[Newtype HashedPassword Text]
                     HashedPassword
                     'Prim,
                   'DdK ('SelSymbol "expiry") '[Nullable] (Maybe Datetime) 'Prim])]))
accountAuth