{-# options_haddock prune #-}

-- | Description: Interpreter for the query for an account by name
module Polysemy.Account.Interpreter.AccountByName where

import Polysemy.Db (DbError, PureStore, Query, Store, interpretQueryStoreConc)
import Sqel (Uid (Uid))

import Polysemy.Account.Data.Account (Account (Account))
import Polysemy.Account.Data.AccountByName (AccountByName (AccountByName))

match ::
  AccountByName ->
  Uid i (Account p) ->
  Maybe (Uid i (Account p))
match :: forall i p.
AccountByName -> Uid i (Account p) -> Maybe (Uid i (Account p))
match (AccountByName AccountName
name) a :: Uid i (Account p)
a@(Uid i
_ (Account AccountName
accountName AccountStatus
_ p
_))
  | AccountName
name forall a. Eq a => a -> a -> Bool
== AccountName
accountName = forall a. a -> Maybe a
Just Uid i (Account p)
a
  | Bool
otherwise = forall a. Maybe a
Nothing

-- | The effects handled by 'interpretAccountByNameState'.
type AccountQuery i p =
  [
    Query AccountByName (Maybe (Uid i (Account p))) !! DbError,
    Store i (Account p) !! DbError,
    AtomicState (PureStore i (Account p))
  ]

-- | Interpret @'Query' 'AccountByName'@ and the corresponding 'Store' in an 'AtomicState'.
interpretAccountByNameState ::
   i p r .
  Ord i =>
  Show i =>
  Member (Embed IO) r =>
  [Uid i (Account p)] ->
  InterpretersFor (AccountQuery i p) r
interpretAccountByNameState :: forall i p (r :: EffectRow).
(Ord i, Show i, Member (Embed IO) r) =>
[Uid i (Account p)] -> InterpretersFor (AccountQuery i p) r
interpretAccountByNameState [Uid i (Account p)]
initial =
  forall i (f :: * -> *) (r :: EffectRow) q a d.
(Ord i, Show i, QueryCheckResult f, Member (Embed IO) r) =>
(q -> Uid i a -> Maybe d)
-> [Uid i a]
-> InterpretersFor
     '[Query q (f d) !! DbError, Store i a !! DbError,
       AtomicState (PureStore i a)]
     r
interpretQueryStoreConc forall i p.
AccountByName -> Uid i (Account p) -> Maybe (Uid i (Account p))
match [Uid i (Account p)]
initial