-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2025 Wire Swiss GmbH <opensource@wire.com>
--
-- This program is free software: you can redistribute it and/or modify it under
-- the terms of the GNU Affero General Public License as published by the Free
-- Software Foundation, either version 3 of the License, or (at your option) any
-- later version.
--
-- This program is distributed in the hope that it will be useful, but WITHOUT
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
-- details.
--
-- You should have received a copy of the GNU Affero General Public License along
-- with this program. If not, see <https://www.gnu.org/licenses/>.

module Wire.API.PostgresMarshall
  ( PostgresMarshall (..),
    PostgresUnmarshall (..),
    StoreAsJSON (..),
    lmapPG,
    rmapPG,
    dimapPG,
  )
where

import Data.Aeson
import Data.Bifunctor (first)
import Data.ByteString qualified as BS
import Data.ByteString.Conversion (toByteString')
import Data.ByteString.Conversion qualified as BSC
import Data.Code qualified as Code
import Data.Domain
import Data.Handle
import Data.Id
import Data.Json.Util (UTCTimeMillis (fromUTCTimeMillis), toUTCTimeMillis)
import Data.Misc
import Data.Profunctor
import Data.Set qualified as Set
import Data.Text qualified as Text
import Data.Text.Ascii qualified as Ascii
import Data.Text.Encoding qualified as Text
import Data.Time (UTCTime)
import Data.UUID
import Data.Vector (Vector)
import Data.Vector qualified as V
import Hasql.Statement
import Imports
import SAML2.WebSSO qualified as SAML
import Wire.API.EnterpriseLogin

class PostgresMarshall db domain where
  postgresMarshall :: domain -> db

instance {-# OVERLAPPABLE #-} (a ~ b) => PostgresMarshall a b where
  postgresMarshall = id

instance (PostgresMarshall a1 b1, PostgresMarshall a2 b2) => PostgresMarshall (a1, a2) (b1, b2) where
  postgresMarshall (a1, a2) = (postgresMarshall a1, postgresMarshall a2)

instance
  ( PostgresMarshall a1 b1,
    PostgresMarshall a2 b2,
    PostgresMarshall a3 b3
  ) =>
  PostgresMarshall (a1, a2, a3) (b1, b2, b3)
  where
  postgresMarshall (a1, a2, a3) = (postgresMarshall a1, postgresMarshall a2, postgresMarshall a3)

instance
  ( PostgresMarshall a1 b1,
    PostgresMarshall a2 b2,
    PostgresMarshall a3 b3,
    PostgresMarshall a4 b4
  ) =>
  PostgresMarshall (a1, a2, a3, a4) (b1, b2, b3, b4)
  where
  postgresMarshall (a1, a2, a3, a4) =
    ( postgresMarshall a1,
      postgresMarshall a2,
      postgresMarshall a3,
      postgresMarshall a4
    )

instance
  ( PostgresMarshall a1 b1,
    PostgresMarshall a2 b2,
    PostgresMarshall a3 b3,
    PostgresMarshall a4 b4,
    PostgresMarshall a5 b5
  ) =>
  PostgresMarshall (a1, a2, a3, a4, a5) (b1, b2, b3, b4, b5)
  where
  postgresMarshall (a1, a2, a3, a4, a5) =
    ( postgresMarshall a1,
      postgresMarshall a2,
      postgresMarshall a3,
      postgresMarshall a4,
      postgresMarshall a5
    )

instance
  ( PostgresMarshall a1 b1,
    PostgresMarshall a2 b2,
    PostgresMarshall a3 b3,
    PostgresMarshall a4 b4,
    PostgresMarshall a5 b5,
    PostgresMarshall a6 b6
  ) =>
  PostgresMarshall (a1, a2, a3, a4, a5, a6) (b1, b2, b3, b4, b5, b6)
  where
  postgresMarshall (a1, a2, a3, a4, a5, a6) =
    ( postgresMarshall a1,
      postgresMarshall a2,
      postgresMarshall a3,
      postgresMarshall a4,
      postgresMarshall a5,
      postgresMarshall a6
    )

instance
  ( PostgresMarshall a1 b1,
    PostgresMarshall a2 b2,
    PostgresMarshall a3 b3,
    PostgresMarshall a4 b4,
    PostgresMarshall a5 b5,
    PostgresMarshall a6 b6,
    PostgresMarshall a7 b7
  ) =>
  PostgresMarshall (a1, a2, a3, a4, a5, a6, a7) (b1, b2, b3, b4, b5, b6, b7)
  where
  postgresMarshall (a1, a2, a3, a4, a5, a6, a7) =
    ( postgresMarshall a1,
      postgresMarshall a2,
      postgresMarshall a3,
      postgresMarshall a4,
      postgresMarshall a5,
      postgresMarshall a6,
      postgresMarshall a7
    )

instance
  ( PostgresMarshall a1 b1,
    PostgresMarshall a2 b2,
    PostgresMarshall a3 b3,
    PostgresMarshall a4 b4,
    PostgresMarshall a5 b5,
    PostgresMarshall a6 b6,
    PostgresMarshall a7 b7,
    PostgresMarshall a8 b8
  ) =>
  PostgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8) (b1, b2, b3, b4, b5, b6, b7, b8)
  where
  postgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8) =
    ( postgresMarshall a1,
      postgresMarshall a2,
      postgresMarshall a3,
      postgresMarshall a4,
      postgresMarshall a5,
      postgresMarshall a6,
      postgresMarshall a7,
      postgresMarshall a8
    )

instance
  ( PostgresMarshall a1 b1,
    PostgresMarshall a2 b2,
    PostgresMarshall a3 b3,
    PostgresMarshall a4 b4,
    PostgresMarshall a5 b5,
    PostgresMarshall a6 b6,
    PostgresMarshall a7 b7,
    PostgresMarshall a8 b8,
    PostgresMarshall a9 b9
  ) =>
  PostgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9) (b1, b2, b3, b4, b5, b6, b7, b8, b9)
  where
  postgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9) =
    ( postgresMarshall a1,
      postgresMarshall a2,
      postgresMarshall a3,
      postgresMarshall a4,
      postgresMarshall a5,
      postgresMarshall a6,
      postgresMarshall a7,
      postgresMarshall a8,
      postgresMarshall a9
    )

instance
  ( PostgresMarshall a1 b1,
    PostgresMarshall a2 b2,
    PostgresMarshall a3 b3,
    PostgresMarshall a4 b4,
    PostgresMarshall a5 b5,
    PostgresMarshall a6 b6,
    PostgresMarshall a7 b7,
    PostgresMarshall a8 b8,
    PostgresMarshall a9 b9,
    PostgresMarshall a10 b10
  ) =>
  PostgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10)
  where
  postgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) =
    ( postgresMarshall a1,
      postgresMarshall a2,
      postgresMarshall a3,
      postgresMarshall a4,
      postgresMarshall a5,
      postgresMarshall a6,
      postgresMarshall a7,
      postgresMarshall a8,
      postgresMarshall a9,
      postgresMarshall a10
    )

instance
  ( PostgresMarshall a1 b1,
    PostgresMarshall a2 b2,
    PostgresMarshall a3 b3,
    PostgresMarshall a4 b4,
    PostgresMarshall a5 b5,
    PostgresMarshall a6 b6,
    PostgresMarshall a7 b7,
    PostgresMarshall a8 b8,
    PostgresMarshall a9 b9,
    PostgresMarshall a10 b10,
    PostgresMarshall a11 b11
  ) =>
  PostgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11)
  where
  postgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) =
    ( postgresMarshall a1,
      postgresMarshall a2,
      postgresMarshall a3,
      postgresMarshall a4,
      postgresMarshall a5,
      postgresMarshall a6,
      postgresMarshall a7,
      postgresMarshall a8,
      postgresMarshall a9,
      postgresMarshall a10,
      postgresMarshall a11
    )

instance
  ( PostgresMarshall a1 b1,
    PostgresMarshall a2 b2,
    PostgresMarshall a3 b3,
    PostgresMarshall a4 b4,
    PostgresMarshall a5 b5,
    PostgresMarshall a6 b6,
    PostgresMarshall a7 b7,
    PostgresMarshall a8 b8,
    PostgresMarshall a9 b9,
    PostgresMarshall a10 b10,
    PostgresMarshall a11 b11,
    PostgresMarshall a12 b12
  ) =>
  PostgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12)
  where
  postgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) =
    ( postgresMarshall a1,
      postgresMarshall a2,
      postgresMarshall a3,
      postgresMarshall a4,
      postgresMarshall a5,
      postgresMarshall a6,
      postgresMarshall a7,
      postgresMarshall a8,
      postgresMarshall a9,
      postgresMarshall a10,
      postgresMarshall a11,
      postgresMarshall a12
    )

instance
  ( PostgresMarshall a1 b1,
    PostgresMarshall a2 b2,
    PostgresMarshall a3 b3,
    PostgresMarshall a4 b4,
    PostgresMarshall a5 b5,
    PostgresMarshall a6 b6,
    PostgresMarshall a7 b7,
    PostgresMarshall a8 b8,
    PostgresMarshall a9 b9,
    PostgresMarshall a10 b10,
    PostgresMarshall a11 b11,
    PostgresMarshall a12 b12,
    PostgresMarshall a13 b13
  ) =>
  PostgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13)
  where
  postgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) =
    ( postgresMarshall a1,
      postgresMarshall a2,
      postgresMarshall a3,
      postgresMarshall a4,
      postgresMarshall a5,
      postgresMarshall a6,
      postgresMarshall a7,
      postgresMarshall a8,
      postgresMarshall a9,
      postgresMarshall a10,
      postgresMarshall a11,
      postgresMarshall a12,
      postgresMarshall a13
    )

instance
  ( PostgresMarshall a1 b1,
    PostgresMarshall a2 b2,
    PostgresMarshall a3 b3,
    PostgresMarshall a4 b4,
    PostgresMarshall a5 b5,
    PostgresMarshall a6 b6,
    PostgresMarshall a7 b7,
    PostgresMarshall a8 b8,
    PostgresMarshall a9 b9,
    PostgresMarshall a10 b10,
    PostgresMarshall a11 b11,
    PostgresMarshall a12 b12,
    PostgresMarshall a13 b13,
    PostgresMarshall a14 b14
  ) =>
  PostgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14)
  where
  postgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) =
    ( postgresMarshall a1,
      postgresMarshall a2,
      postgresMarshall a3,
      postgresMarshall a4,
      postgresMarshall a5,
      postgresMarshall a6,
      postgresMarshall a7,
      postgresMarshall a8,
      postgresMarshall a9,
      postgresMarshall a10,
      postgresMarshall a11,
      postgresMarshall a12,
      postgresMarshall a13,
      postgresMarshall a14
    )

instance
  ( PostgresMarshall a1 b1,
    PostgresMarshall a2 b2,
    PostgresMarshall a3 b3,
    PostgresMarshall a4 b4,
    PostgresMarshall a5 b5,
    PostgresMarshall a6 b6,
    PostgresMarshall a7 b7,
    PostgresMarshall a8 b8,
    PostgresMarshall a9 b9,
    PostgresMarshall a10 b10,
    PostgresMarshall a11 b11,
    PostgresMarshall a12 b12,
    PostgresMarshall a13 b13,
    PostgresMarshall a14 b14,
    PostgresMarshall a15 b15
  ) =>
  PostgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15)
  where
  postgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) =
    ( postgresMarshall a1,
      postgresMarshall a2,
      postgresMarshall a3,
      postgresMarshall a4,
      postgresMarshall a5,
      postgresMarshall a6,
      postgresMarshall a7,
      postgresMarshall a8,
      postgresMarshall a9,
      postgresMarshall a10,
      postgresMarshall a11,
      postgresMarshall a12,
      postgresMarshall a13,
      postgresMarshall a14,
      postgresMarshall a15
    )

instance
  ( PostgresMarshall a1 b1,
    PostgresMarshall a2 b2,
    PostgresMarshall a3 b3,
    PostgresMarshall a4 b4,
    PostgresMarshall a5 b5,
    PostgresMarshall a6 b6,
    PostgresMarshall a7 b7,
    PostgresMarshall a8 b8,
    PostgresMarshall a9 b9,
    PostgresMarshall a10 b10,
    PostgresMarshall a11 b11,
    PostgresMarshall a12 b12,
    PostgresMarshall a13 b13,
    PostgresMarshall a14 b14,
    PostgresMarshall a15 b15,
    PostgresMarshall a16 b16
  ) =>
  PostgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16)
  where
  postgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) =
    ( postgresMarshall a1,
      postgresMarshall a2,
      postgresMarshall a3,
      postgresMarshall a4,
      postgresMarshall a5,
      postgresMarshall a6,
      postgresMarshall a7,
      postgresMarshall a8,
      postgresMarshall a9,
      postgresMarshall a10,
      postgresMarshall a11,
      postgresMarshall a12,
      postgresMarshall a13,
      postgresMarshall a14,
      postgresMarshall a15,
      postgresMarshall a16
    )

instance (PostgresMarshall a1 b1, PostgresMarshall a2 b2, PostgresMarshall a3 b3, PostgresMarshall a4 b4, PostgresMarshall a5 b5, PostgresMarshall a6 b6, PostgresMarshall a7 b7, PostgresMarshall a8 b8, PostgresMarshall a9 b9, PostgresMarshall a10 b10, PostgresMarshall a11 b11, PostgresMarshall a12 b12, PostgresMarshall a13 b13, PostgresMarshall a14 b14, PostgresMarshall a15 b15, PostgresMarshall a16 b16, PostgresMarshall a17 b17) => PostgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17) where
  postgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17) =
    ( postgresMarshall a1,
      postgresMarshall a2,
      postgresMarshall a3,
      postgresMarshall a4,
      postgresMarshall a5,
      postgresMarshall a6,
      postgresMarshall a7,
      postgresMarshall a8,
      postgresMarshall a9,
      postgresMarshall a10,
      postgresMarshall a11,
      postgresMarshall a12,
      postgresMarshall a13,
      postgresMarshall a14,
      postgresMarshall a15,
      postgresMarshall a16,
      postgresMarshall a17
    )

instance (PostgresMarshall a1 b1, PostgresMarshall a2 b2, PostgresMarshall a3 b3, PostgresMarshall a4 b4, PostgresMarshall a5 b5, PostgresMarshall a6 b6, PostgresMarshall a7 b7, PostgresMarshall a8 b8, PostgresMarshall a9 b9, PostgresMarshall a10 b10, PostgresMarshall a11 b11, PostgresMarshall a12 b12, PostgresMarshall a13 b13, PostgresMarshall a14 b14, PostgresMarshall a15 b15, PostgresMarshall a16 b16, PostgresMarshall a17 b17, PostgresMarshall a18 b18) => PostgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18) where
  postgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18) =
    ( postgresMarshall a1,
      postgresMarshall a2,
      postgresMarshall a3,
      postgresMarshall a4,
      postgresMarshall a5,
      postgresMarshall a6,
      postgresMarshall a7,
      postgresMarshall a8,
      postgresMarshall a9,
      postgresMarshall a10,
      postgresMarshall a11,
      postgresMarshall a12,
      postgresMarshall a13,
      postgresMarshall a14,
      postgresMarshall a15,
      postgresMarshall a16,
      postgresMarshall a17,
      postgresMarshall a18
    )

instance (PostgresMarshall a1 b1, PostgresMarshall a2 b2, PostgresMarshall a3 b3, PostgresMarshall a4 b4, PostgresMarshall a5 b5, PostgresMarshall a6 b6, PostgresMarshall a7 b7, PostgresMarshall a8 b8, PostgresMarshall a9 b9, PostgresMarshall a10 b10, PostgresMarshall a11 b11, PostgresMarshall a12 b12, PostgresMarshall a13 b13, PostgresMarshall a14 b14, PostgresMarshall a15 b15, PostgresMarshall a16 b16, PostgresMarshall a17 b17, PostgresMarshall a18 b18, PostgresMarshall a19 b19) => PostgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19) where
  postgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19) =
    ( postgresMarshall a1,
      postgresMarshall a2,
      postgresMarshall a3,
      postgresMarshall a4,
      postgresMarshall a5,
      postgresMarshall a6,
      postgresMarshall a7,
      postgresMarshall a8,
      postgresMarshall a9,
      postgresMarshall a10,
      postgresMarshall a11,
      postgresMarshall a12,
      postgresMarshall a13,
      postgresMarshall a14,
      postgresMarshall a15,
      postgresMarshall a16,
      postgresMarshall a17,
      postgresMarshall a18,
      postgresMarshall a19
    )

instance (PostgresMarshall a1 b1, PostgresMarshall a2 b2, PostgresMarshall a3 b3, PostgresMarshall a4 b4, PostgresMarshall a5 b5, PostgresMarshall a6 b6, PostgresMarshall a7 b7, PostgresMarshall a8 b8, PostgresMarshall a9 b9, PostgresMarshall a10 b10, PostgresMarshall a11 b11, PostgresMarshall a12 b12, PostgresMarshall a13 b13, PostgresMarshall a14 b14, PostgresMarshall a15 b15, PostgresMarshall a16 b16, PostgresMarshall a17 b17, PostgresMarshall a18 b18, PostgresMarshall a19 b19, PostgresMarshall a20 b20) => PostgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20) where
  postgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) =
    ( postgresMarshall a1,
      postgresMarshall a2,
      postgresMarshall a3,
      postgresMarshall a4,
      postgresMarshall a5,
      postgresMarshall a6,
      postgresMarshall a7,
      postgresMarshall a8,
      postgresMarshall a9,
      postgresMarshall a10,
      postgresMarshall a11,
      postgresMarshall a12,
      postgresMarshall a13,
      postgresMarshall a14,
      postgresMarshall a15,
      postgresMarshall a16,
      postgresMarshall a17,
      postgresMarshall a18,
      postgresMarshall a19,
      postgresMarshall a20
    )

instance (PostgresMarshall a1 b1, PostgresMarshall a2 b2, PostgresMarshall a3 b3, PostgresMarshall a4 b4, PostgresMarshall a5 b5, PostgresMarshall a6 b6, PostgresMarshall a7 b7, PostgresMarshall a8 b8, PostgresMarshall a9 b9, PostgresMarshall a10 b10, PostgresMarshall a11 b11, PostgresMarshall a12 b12, PostgresMarshall a13 b13, PostgresMarshall a14 b14, PostgresMarshall a15 b15, PostgresMarshall a16 b16, PostgresMarshall a17 b17, PostgresMarshall a18 b18, PostgresMarshall a19 b19, PostgresMarshall a20 b20, PostgresMarshall a21 b21) => PostgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21) where
  postgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21) = (postgresMarshall a1, postgresMarshall a2, postgresMarshall a3, postgresMarshall a4, postgresMarshall a5, postgresMarshall a6, postgresMarshall a7, postgresMarshall a8, postgresMarshall a9, postgresMarshall a10, postgresMarshall a11, postgresMarshall a12, postgresMarshall a13, postgresMarshall a14, postgresMarshall a15, postgresMarshall a16, postgresMarshall a17, postgresMarshall a18, postgresMarshall a19, postgresMarshall a20, postgresMarshall a21)

instance (PostgresMarshall a1 b1, PostgresMarshall a2 b2, PostgresMarshall a3 b3, PostgresMarshall a4 b4, PostgresMarshall a5 b5, PostgresMarshall a6 b6, PostgresMarshall a7 b7, PostgresMarshall a8 b8, PostgresMarshall a9 b9, PostgresMarshall a10 b10, PostgresMarshall a11 b11, PostgresMarshall a12 b12, PostgresMarshall a13 b13, PostgresMarshall a14 b14, PostgresMarshall a15 b15, PostgresMarshall a16 b16, PostgresMarshall a17 b17, PostgresMarshall a18 b18, PostgresMarshall a19 b19, PostgresMarshall a20 b20, PostgresMarshall a21 b21, PostgresMarshall a22 b22) => PostgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22) where
  postgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22) = (postgresMarshall a1, postgresMarshall a2, postgresMarshall a3, postgresMarshall a4, postgresMarshall a5, postgresMarshall a6, postgresMarshall a7, postgresMarshall a8, postgresMarshall a9, postgresMarshall a10, postgresMarshall a11, postgresMarshall a12, postgresMarshall a13, postgresMarshall a14, postgresMarshall a15, postgresMarshall a16, postgresMarshall a17, postgresMarshall a18, postgresMarshall a19, postgresMarshall a20, postgresMarshall a21, postgresMarshall a22)

instance (PostgresMarshall a1 b1, PostgresMarshall a2 b2, PostgresMarshall a3 b3, PostgresMarshall a4 b4, PostgresMarshall a5 b5, PostgresMarshall a6 b6, PostgresMarshall a7 b7, PostgresMarshall a8 b8, PostgresMarshall a9 b9, PostgresMarshall a10 b10, PostgresMarshall a11 b11, PostgresMarshall a12 b12, PostgresMarshall a13 b13, PostgresMarshall a14 b14, PostgresMarshall a15 b15, PostgresMarshall a16 b16, PostgresMarshall a17 b17, PostgresMarshall a18 b18, PostgresMarshall a19 b19, PostgresMarshall a20 b20, PostgresMarshall a21 b21, PostgresMarshall a22 b22, PostgresMarshall a23 b23) => PostgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23) where
  postgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23) = (postgresMarshall a1, postgresMarshall a2, postgresMarshall a3, postgresMarshall a4, postgresMarshall a5, postgresMarshall a6, postgresMarshall a7, postgresMarshall a8, postgresMarshall a9, postgresMarshall a10, postgresMarshall a11, postgresMarshall a12, postgresMarshall a13, postgresMarshall a14, postgresMarshall a15, postgresMarshall a16, postgresMarshall a17, postgresMarshall a18, postgresMarshall a19, postgresMarshall a20, postgresMarshall a21, postgresMarshall a22, postgresMarshall a23)

instance (PostgresMarshall a1 b1, PostgresMarshall a2 b2, PostgresMarshall a3 b3, PostgresMarshall a4 b4, PostgresMarshall a5 b5, PostgresMarshall a6 b6, PostgresMarshall a7 b7, PostgresMarshall a8 b8, PostgresMarshall a9 b9, PostgresMarshall a10 b10, PostgresMarshall a11 b11, PostgresMarshall a12 b12, PostgresMarshall a13 b13, PostgresMarshall a14 b14, PostgresMarshall a15 b15, PostgresMarshall a16 b16, PostgresMarshall a17 b17, PostgresMarshall a18 b18, PostgresMarshall a19 b19, PostgresMarshall a20 b20, PostgresMarshall a21 b21, PostgresMarshall a22 b22, PostgresMarshall a23 b23, PostgresMarshall a24 b24) => PostgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23, a24) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24) where
  postgresMarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23, a24) = (postgresMarshall a1, postgresMarshall a2, postgresMarshall a3, postgresMarshall a4, postgresMarshall a5, postgresMarshall a6, postgresMarshall a7, postgresMarshall a8, postgresMarshall a9, postgresMarshall a10, postgresMarshall a11, postgresMarshall a12, postgresMarshall a13, postgresMarshall a14, postgresMarshall a15, postgresMarshall a16, postgresMarshall a17, postgresMarshall a18, postgresMarshall a19, postgresMarshall a20, postgresMarshall a21, postgresMarshall a22, postgresMarshall a23, postgresMarshall a24)

instance PostgresMarshall UUID (Id a) where
  postgresMarshall = toUUID

instance PostgresMarshall UUID BotId where
  postgresMarshall = toUUID . botUserId

instance PostgresMarshall Text ClientId where
  postgresMarshall = clientToText

instance PostgresMarshall Value Object where
  postgresMarshall = Object

instance PostgresMarshall Int64 Milliseconds where
  postgresMarshall = msToInt64

instance PostgresMarshall Text Domain where
  postgresMarshall = domainText

instance PostgresMarshall Text Handle where
  postgresMarshall = fromHandle

instance PostgresMarshall UTCTime UTCTimeMillis where
  postgresMarshall = fromUTCTimeMillis

instance (PostgresMarshall a b) => PostgresMarshall (Maybe a) (Maybe b) where
  postgresMarshall = fmap postgresMarshall

instance (PostgresMarshall b a) => PostgresMarshall (Vector b) [a] where
  postgresMarshall = V.fromList . map postgresMarshall

instance (PostgresMarshall b a) => PostgresMarshall (Vector b) (Set a) where
  postgresMarshall = V.fromList . map postgresMarshall . Set.toList

instance (PostgresMarshall a b) => PostgresMarshall (Vector a) (Vector b) where
  postgresMarshall = V.map postgresMarshall

instance PostgresMarshall Text Code.Key where
  postgresMarshall = Text.decodeUtf8 . toByteString'

instance PostgresMarshall Text Code.Value where
  postgresMarshall = Text.decodeUtf8 . toByteString'

instance PostgresMarshall ByteString HttpsUrl where
  postgresMarshall = toByteString'

instance PostgresMarshall ByteString Token where
  postgresMarshall = (.unToken)

instance PostgresMarshall Text DnsVerificationToken where
  postgresMarshall = Ascii.toText . (.unDnsVerificationToken)

instance PostgresMarshall Int32 DomainRedirectTag where
  postgresMarshall = \case
    NoneTag -> 1
    LockedTag -> 2
    SSOTag -> 3
    BackendTag -> 4
    NoRegistrationTag -> 5
    PreAuthorizedTag -> 6

instance PostgresMarshall Int32 TeamInviteTag where
  postgresMarshall = \case
    AllowedTag -> 1
    NotAllowedTag -> 2
    TeamTag -> 3

instance PostgresMarshall UUID SAML.IdPId where
  postgresMarshall = SAML.fromIdPId

---

class PostgresUnmarshall db domain where
  postgresUnmarshall :: db -> Either Text domain

instance {-# OVERLAPPABLE #-} (a ~ b) => PostgresUnmarshall a b where
  postgresUnmarshall = Right

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2) => PostgresUnmarshall (a1, a2) (b1, b2) where
  postgresUnmarshall (a1, a2) =
    (,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3) => PostgresUnmarshall (a1, a2, a3) (b1, b2, b3) where
  postgresUnmarshall (a1, a2, a3) =
    (,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4) => PostgresUnmarshall (a1, a2, a3, a4) (b1, b2, b3, b4) where
  postgresUnmarshall (a1, a2, a3, a4) =
    (,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5) => PostgresUnmarshall (a1, a2, a3, a4, a5) (b1, b2, b3, b4, b5) where
  postgresUnmarshall (a1, a2, a3, a4, a5) =
    (,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6) (b1, b2, b3, b4, b5, b6) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6) =
    (,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6, PostgresUnmarshall a7 b7) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6, a7) (b1, b2, b3, b4, b5, b6, b7) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6, a7) =
    (,,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6
      <*> postgresUnmarshall a7

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6, PostgresUnmarshall a7 b7, PostgresUnmarshall a8 b8) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8) (b1, b2, b3, b4, b5, b6, b7, b8) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8) =
    (,,,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6
      <*> postgresUnmarshall a7
      <*> postgresUnmarshall a8

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6, PostgresUnmarshall a7 b7, PostgresUnmarshall a8 b8, PostgresUnmarshall a9 b9) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9) (b1, b2, b3, b4, b5, b6, b7, b8, b9) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9) =
    (,,,,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6
      <*> postgresUnmarshall a7
      <*> postgresUnmarshall a8
      <*> postgresUnmarshall a9

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6, PostgresUnmarshall a7 b7, PostgresUnmarshall a8 b8, PostgresUnmarshall a9 b9, PostgresUnmarshall a10 b10) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) =
    (,,,,,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6
      <*> postgresUnmarshall a7
      <*> postgresUnmarshall a8
      <*> postgresUnmarshall a9
      <*> postgresUnmarshall a10

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6, PostgresUnmarshall a7 b7, PostgresUnmarshall a8 b8, PostgresUnmarshall a9 b9, PostgresUnmarshall a10 b10, PostgresUnmarshall a11 b11) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) =
    (,,,,,,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6
      <*> postgresUnmarshall a7
      <*> postgresUnmarshall a8
      <*> postgresUnmarshall a9
      <*> postgresUnmarshall a10
      <*> postgresUnmarshall a11

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6, PostgresUnmarshall a7 b7, PostgresUnmarshall a8 b8, PostgresUnmarshall a9 b9, PostgresUnmarshall a10 b10, PostgresUnmarshall a11 b11, PostgresUnmarshall a12 b12) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) =
    (,,,,,,,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6
      <*> postgresUnmarshall a7
      <*> postgresUnmarshall a8
      <*> postgresUnmarshall a9
      <*> postgresUnmarshall a10
      <*> postgresUnmarshall a11
      <*> postgresUnmarshall a12

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6, PostgresUnmarshall a7 b7, PostgresUnmarshall a8 b8, PostgresUnmarshall a9 b9, PostgresUnmarshall a10 b10, PostgresUnmarshall a11 b11, PostgresUnmarshall a12 b12, PostgresUnmarshall a13 b13) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) =
    (,,,,,,,,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6
      <*> postgresUnmarshall a7
      <*> postgresUnmarshall a8
      <*> postgresUnmarshall a9
      <*> postgresUnmarshall a10
      <*> postgresUnmarshall a11
      <*> postgresUnmarshall a12
      <*> postgresUnmarshall a13

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6, PostgresUnmarshall a7 b7, PostgresUnmarshall a8 b8, PostgresUnmarshall a9 b9, PostgresUnmarshall a10 b10, PostgresUnmarshall a11 b11, PostgresUnmarshall a12 b12, PostgresUnmarshall a13 b13, PostgresUnmarshall a14 b14) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) =
    (,,,,,,,,,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6
      <*> postgresUnmarshall a7
      <*> postgresUnmarshall a8
      <*> postgresUnmarshall a9
      <*> postgresUnmarshall a10
      <*> postgresUnmarshall a11
      <*> postgresUnmarshall a12
      <*> postgresUnmarshall a13
      <*> postgresUnmarshall a14

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6, PostgresUnmarshall a7 b7, PostgresUnmarshall a8 b8, PostgresUnmarshall a9 b9, PostgresUnmarshall a10 b10, PostgresUnmarshall a11 b11, PostgresUnmarshall a12 b12, PostgresUnmarshall a13 b13, PostgresUnmarshall a14 b14, PostgresUnmarshall a15 b15) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) =
    (,,,,,,,,,,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6
      <*> postgresUnmarshall a7
      <*> postgresUnmarshall a8
      <*> postgresUnmarshall a9
      <*> postgresUnmarshall a10
      <*> postgresUnmarshall a11
      <*> postgresUnmarshall a12
      <*> postgresUnmarshall a13
      <*> postgresUnmarshall a14
      <*> postgresUnmarshall a15

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6, PostgresUnmarshall a7 b7, PostgresUnmarshall a8 b8, PostgresUnmarshall a9 b9, PostgresUnmarshall a10 b10, PostgresUnmarshall a11 b11, PostgresUnmarshall a12 b12, PostgresUnmarshall a13 b13, PostgresUnmarshall a14 b14, PostgresUnmarshall a15 b15, PostgresUnmarshall a16 b16) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) =
    (,,,,,,,,,,,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6
      <*> postgresUnmarshall a7
      <*> postgresUnmarshall a8
      <*> postgresUnmarshall a9
      <*> postgresUnmarshall a10
      <*> postgresUnmarshall a11
      <*> postgresUnmarshall a12
      <*> postgresUnmarshall a13
      <*> postgresUnmarshall a14
      <*> postgresUnmarshall a15
      <*> postgresUnmarshall a16

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6, PostgresUnmarshall a7 b7, PostgresUnmarshall a8 b8, PostgresUnmarshall a9 b9, PostgresUnmarshall a10 b10, PostgresUnmarshall a11 b11, PostgresUnmarshall a12 b12, PostgresUnmarshall a13 b13, PostgresUnmarshall a14 b14, PostgresUnmarshall a15 b15, PostgresUnmarshall a16 b16, PostgresUnmarshall a17 b17) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17) =
    (,,,,,,,,,,,,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6
      <*> postgresUnmarshall a7
      <*> postgresUnmarshall a8
      <*> postgresUnmarshall a9
      <*> postgresUnmarshall a10
      <*> postgresUnmarshall a11
      <*> postgresUnmarshall a12
      <*> postgresUnmarshall a13
      <*> postgresUnmarshall a14
      <*> postgresUnmarshall a15
      <*> postgresUnmarshall a16
      <*> postgresUnmarshall a17

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6, PostgresUnmarshall a7 b7, PostgresUnmarshall a8 b8, PostgresUnmarshall a9 b9, PostgresUnmarshall a10 b10, PostgresUnmarshall a11 b11, PostgresUnmarshall a12 b12, PostgresUnmarshall a13 b13, PostgresUnmarshall a14 b14, PostgresUnmarshall a15 b15, PostgresUnmarshall a16 b16, PostgresUnmarshall a17 b17, PostgresUnmarshall a18 b18) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18) =
    (,,,,,,,,,,,,,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6
      <*> postgresUnmarshall a7
      <*> postgresUnmarshall a8
      <*> postgresUnmarshall a9
      <*> postgresUnmarshall a10
      <*> postgresUnmarshall a11
      <*> postgresUnmarshall a12
      <*> postgresUnmarshall a13
      <*> postgresUnmarshall a14
      <*> postgresUnmarshall a15
      <*> postgresUnmarshall a16
      <*> postgresUnmarshall a17
      <*> postgresUnmarshall a18

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6, PostgresUnmarshall a7 b7, PostgresUnmarshall a8 b8, PostgresUnmarshall a9 b9, PostgresUnmarshall a10 b10, PostgresUnmarshall a11 b11, PostgresUnmarshall a12 b12, PostgresUnmarshall a13 b13, PostgresUnmarshall a14 b14, PostgresUnmarshall a15 b15, PostgresUnmarshall a16 b16, PostgresUnmarshall a17 b17, PostgresUnmarshall a18 b18, PostgresUnmarshall a19 b19) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19) =
    (,,,,,,,,,,,,,,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6
      <*> postgresUnmarshall a7
      <*> postgresUnmarshall a8
      <*> postgresUnmarshall a9
      <*> postgresUnmarshall a10
      <*> postgresUnmarshall a11
      <*> postgresUnmarshall a12
      <*> postgresUnmarshall a13
      <*> postgresUnmarshall a14
      <*> postgresUnmarshall a15
      <*> postgresUnmarshall a16
      <*> postgresUnmarshall a17
      <*> postgresUnmarshall a18
      <*> postgresUnmarshall a19

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6, PostgresUnmarshall a7 b7, PostgresUnmarshall a8 b8, PostgresUnmarshall a9 b9, PostgresUnmarshall a10 b10, PostgresUnmarshall a11 b11, PostgresUnmarshall a12 b12, PostgresUnmarshall a13 b13, PostgresUnmarshall a14 b14, PostgresUnmarshall a15 b15, PostgresUnmarshall a16 b16, PostgresUnmarshall a17 b17, PostgresUnmarshall a18 b18, PostgresUnmarshall a19 b19, PostgresUnmarshall a20 b20) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) =
    (,,,,,,,,,,,,,,,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6
      <*> postgresUnmarshall a7
      <*> postgresUnmarshall a8
      <*> postgresUnmarshall a9
      <*> postgresUnmarshall a10
      <*> postgresUnmarshall a11
      <*> postgresUnmarshall a12
      <*> postgresUnmarshall a13
      <*> postgresUnmarshall a14
      <*> postgresUnmarshall a15
      <*> postgresUnmarshall a16
      <*> postgresUnmarshall a17
      <*> postgresUnmarshall a18
      <*> postgresUnmarshall a19
      <*> postgresUnmarshall a20

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6, PostgresUnmarshall a7 b7, PostgresUnmarshall a8 b8, PostgresUnmarshall a9 b9, PostgresUnmarshall a10 b10, PostgresUnmarshall a11 b11, PostgresUnmarshall a12 b12, PostgresUnmarshall a13 b13, PostgresUnmarshall a14 b14, PostgresUnmarshall a15 b15, PostgresUnmarshall a16 b16, PostgresUnmarshall a17 b17, PostgresUnmarshall a18 b18, PostgresUnmarshall a19 b19, PostgresUnmarshall a20 b20, PostgresUnmarshall a21 b21) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21) =
    (,,,,,,,,,,,,,,,,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6
      <*> postgresUnmarshall a7
      <*> postgresUnmarshall a8
      <*> postgresUnmarshall a9
      <*> postgresUnmarshall a10
      <*> postgresUnmarshall a11
      <*> postgresUnmarshall a12
      <*> postgresUnmarshall a13
      <*> postgresUnmarshall a14
      <*> postgresUnmarshall a15
      <*> postgresUnmarshall a16
      <*> postgresUnmarshall a17
      <*> postgresUnmarshall a18
      <*> postgresUnmarshall a19
      <*> postgresUnmarshall a20
      <*> postgresUnmarshall a21

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6, PostgresUnmarshall a7 b7, PostgresUnmarshall a8 b8, PostgresUnmarshall a9 b9, PostgresUnmarshall a10 b10, PostgresUnmarshall a11 b11, PostgresUnmarshall a12 b12, PostgresUnmarshall a13 b13, PostgresUnmarshall a14 b14, PostgresUnmarshall a15 b15, PostgresUnmarshall a16 b16, PostgresUnmarshall a17 b17, PostgresUnmarshall a18 b18, PostgresUnmarshall a19 b19, PostgresUnmarshall a20 b20, PostgresUnmarshall a21 b21, PostgresUnmarshall a22 b22) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22) =
    (,,,,,,,,,,,,,,,,,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6
      <*> postgresUnmarshall a7
      <*> postgresUnmarshall a8
      <*> postgresUnmarshall a9
      <*> postgresUnmarshall a10
      <*> postgresUnmarshall a11
      <*> postgresUnmarshall a12
      <*> postgresUnmarshall a13
      <*> postgresUnmarshall a14
      <*> postgresUnmarshall a15
      <*> postgresUnmarshall a16
      <*> postgresUnmarshall a17
      <*> postgresUnmarshall a18
      <*> postgresUnmarshall a19
      <*> postgresUnmarshall a20
      <*> postgresUnmarshall a21
      <*> postgresUnmarshall a22

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6, PostgresUnmarshall a7 b7, PostgresUnmarshall a8 b8, PostgresUnmarshall a9 b9, PostgresUnmarshall a10 b10, PostgresUnmarshall a11 b11, PostgresUnmarshall a12 b12, PostgresUnmarshall a13 b13, PostgresUnmarshall a14 b14, PostgresUnmarshall a15 b15, PostgresUnmarshall a16 b16, PostgresUnmarshall a17 b17, PostgresUnmarshall a18 b18, PostgresUnmarshall a19 b19, PostgresUnmarshall a20 b20, PostgresUnmarshall a21 b21, PostgresUnmarshall a22 b22, PostgresUnmarshall a23 b23) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23) =
    (,,,,,,,,,,,,,,,,,,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6
      <*> postgresUnmarshall a7
      <*> postgresUnmarshall a8
      <*> postgresUnmarshall a9
      <*> postgresUnmarshall a10
      <*> postgresUnmarshall a11
      <*> postgresUnmarshall a12
      <*> postgresUnmarshall a13
      <*> postgresUnmarshall a14
      <*> postgresUnmarshall a15
      <*> postgresUnmarshall a16
      <*> postgresUnmarshall a17
      <*> postgresUnmarshall a18
      <*> postgresUnmarshall a19
      <*> postgresUnmarshall a20
      <*> postgresUnmarshall a21
      <*> postgresUnmarshall a22
      <*> postgresUnmarshall a23

instance (PostgresUnmarshall a1 b1, PostgresUnmarshall a2 b2, PostgresUnmarshall a3 b3, PostgresUnmarshall a4 b4, PostgresUnmarshall a5 b5, PostgresUnmarshall a6 b6, PostgresUnmarshall a7 b7, PostgresUnmarshall a8 b8, PostgresUnmarshall a9 b9, PostgresUnmarshall a10 b10, PostgresUnmarshall a11 b11, PostgresUnmarshall a12 b12, PostgresUnmarshall a13 b13, PostgresUnmarshall a14 b14, PostgresUnmarshall a15 b15, PostgresUnmarshall a16 b16, PostgresUnmarshall a17 b17, PostgresUnmarshall a18 b18, PostgresUnmarshall a19 b19, PostgresUnmarshall a20 b20, PostgresUnmarshall a21 b21, PostgresUnmarshall a22 b22, PostgresUnmarshall a23 b23, PostgresUnmarshall a24 b24) => PostgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23, a24) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24) where
  postgresUnmarshall (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23, a24) =
    (,,,,,,,,,,,,,,,,,,,,,,,)
      <$> postgresUnmarshall a1
      <*> postgresUnmarshall a2
      <*> postgresUnmarshall a3
      <*> postgresUnmarshall a4
      <*> postgresUnmarshall a5
      <*> postgresUnmarshall a6
      <*> postgresUnmarshall a7
      <*> postgresUnmarshall a8
      <*> postgresUnmarshall a9
      <*> postgresUnmarshall a10
      <*> postgresUnmarshall a11
      <*> postgresUnmarshall a12
      <*> postgresUnmarshall a13
      <*> postgresUnmarshall a14
      <*> postgresUnmarshall a15
      <*> postgresUnmarshall a16
      <*> postgresUnmarshall a17
      <*> postgresUnmarshall a18
      <*> postgresUnmarshall a19
      <*> postgresUnmarshall a20
      <*> postgresUnmarshall a21
      <*> postgresUnmarshall a22
      <*> postgresUnmarshall a23
      <*> postgresUnmarshall a24

instance PostgresUnmarshall UUID (Id a) where
  postgresUnmarshall = Right . Id

instance PostgresUnmarshall UUID BotId where
  postgresUnmarshall = Right . BotId . Id

instance PostgresUnmarshall Text ClientId where
  postgresUnmarshall = mapLeft Text.pack . BSC.runParser BSC.parser . Text.encodeUtf8

instance PostgresUnmarshall Value Object where
  postgresUnmarshall (Object obj) = Right obj
  postgresUnmarshall v = Left $ "expected Object, got: " <> Text.decodeUtf8 (BS.toStrict (encode v))

instance PostgresUnmarshall Text Domain where
  postgresUnmarshall = first Text.pack . mkDomain

instance (PostgresUnmarshall a b) => PostgresUnmarshall (Maybe a) (Maybe b) where
  postgresUnmarshall = mapM postgresUnmarshall

instance (PostgresUnmarshall a b) => PostgresUnmarshall (Vector a) (Vector b) where
  postgresUnmarshall = V.mapM postgresUnmarshall

instance (PostgresUnmarshall a b) => PostgresUnmarshall (Vector a) [b] where
  postgresUnmarshall = mapM postgresUnmarshall . V.toList

instance (PostgresUnmarshall a b, Ord b) => PostgresUnmarshall (Vector a) (Set b) where
  postgresUnmarshall =
    fmap Set.fromList . mapM postgresUnmarshall . V.toList

instance PostgresUnmarshall Int64 Milliseconds where
  postgresUnmarshall = Right . int64ToMs

instance PostgresUnmarshall Text Code.Key where
  postgresUnmarshall = mapLeft Text.pack . BSC.runParser BSC.parser . Text.encodeUtf8

instance PostgresUnmarshall Text Code.Value where
  postgresUnmarshall = mapLeft Text.pack . BSC.runParser BSC.parser . Text.encodeUtf8

instance PostgresUnmarshall ByteString HttpsUrl where
  postgresUnmarshall = first Text.pack . BSC.runParser BSC.parser

instance PostgresUnmarshall ByteString Token where
  postgresUnmarshall = Right . Token

instance PostgresUnmarshall Text DnsVerificationToken where
  postgresUnmarshall = first Text.pack . fmap DnsVerificationToken . Ascii.validate

instance PostgresUnmarshall Int32 DomainRedirectTag where
  postgresUnmarshall = \case
    1 -> Right NoneTag
    2 -> Right LockedTag
    3 -> Right SSOTag
    4 -> Right BackendTag
    5 -> Right NoRegistrationTag
    6 -> Right PreAuthorizedTag
    n -> Left $ "Unexpected DomainRedirectTag value: " <> Text.pack (show n)

instance PostgresUnmarshall Int32 TeamInviteTag where
  postgresUnmarshall = \case
    1 -> Right AllowedTag
    2 -> Right NotAllowedTag
    3 -> Right TeamTag
    n -> Left $ "Unexpected TeamInviteTag value: " <> Text.pack (show n)

instance PostgresUnmarshall UUID SAML.IdPId where
  postgresUnmarshall = Right . SAML.IdPId

instance PostgresUnmarshall Text Handle where
  postgresUnmarshall = mapLeft Text.pack . parseHandleEither

instance PostgresUnmarshall UTCTime UTCTimeMillis where
  postgresUnmarshall = Right . toUTCTimeMillis

---

lmapPG :: (PostgresMarshall db domain, Profunctor p) => p db x -> p domain x
lmapPG = lmap postgresMarshall

rmapPG :: (PostgresUnmarshall db domain) => Statement x db -> Statement x domain
rmapPG = refineResult postgresUnmarshall

dimapPG ::
  (PostgresMarshall dbIn domainIn, PostgresUnmarshall dbOut domainOut) =>
  Statement dbIn dbOut ->
  Statement domainIn domainOut
dimapPG = refineResult postgresUnmarshall . lmapPG

---

newtype StoreAsJSON a = StoreAsJSON a

instance (ToJSON a) => PostgresMarshall Value (StoreAsJSON a) where
  postgresMarshall (StoreAsJSON a) = toJSON a

instance (FromJSON a) => PostgresUnmarshall Value (StoreAsJSON a) where
  postgresUnmarshall v =
    case fromJSON v of
      Error e -> Left $ Text.pack e
      Success a -> Right $ StoreAsJSON a
