-- | Description: Interpreter for the query for auth info by account ID
module Polysemy.Account.Interpreter.AuthForAccount where

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

import Polysemy.Account.Data.AccountAuth (AccountAuth (AccountAuth))
import Polysemy.Account.Data.AuthForAccount (AuthForAccount (AuthForAccount))

match ::
  Eq i =>
  AuthForAccount i ->
  Uid i (AccountAuth i) ->
  Maybe (Uid i (AccountAuth i))
match :: forall i.
Eq i =>
AuthForAccount i
-> Uid i (AccountAuth i) -> Maybe (Uid i (AccountAuth i))
match (AuthForAccount i
queryId) a :: Uid i (AccountAuth i)
a@(Uid i
_ (AccountAuth i
accountId AccountAuthDescription
_ HashedPassword
_ Maybe Datetime
_))
  | i
queryId forall a. Eq a => a -> a -> Bool
== i
accountId = forall a. a -> Maybe a
Just Uid i (AccountAuth i)
a
  | Bool
otherwise = forall a. Maybe a
Nothing

type AuthQuery i p =
  [
    Query (AuthForAccount i) [Uid i (AccountAuth i)] !! DbError,
    Store i (AccountAuth i) !! DbError,
    AtomicState (PureStore i (AccountAuth i))
  ]

-- | Interpret @'Query' 'AccountAuth'@ and the corresponding 'Store' in an 'AtomicState'.
interpretAuthForAccountState ::
   i r p .
  Ord i =>
  Show i =>
  Member (Embed IO) r =>
  [Uid i (AccountAuth i)] ->
  InterpretersFor (AuthQuery i p) r
interpretAuthForAccountState :: forall {k} i (r :: EffectRow) (p :: k).
(Ord i, Show i, Member (Embed IO) r) =>
[Uid i (AccountAuth i)] -> InterpretersFor (AuthQuery i p) r
interpretAuthForAccountState [Uid i (AccountAuth i)]
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.
Eq i =>
AuthForAccount i
-> Uid i (AccountAuth i) -> Maybe (Uid i (AccountAuth i))
match [Uid i (AccountAuth i)]
initial