{-# LANGUAGE PackageImports #-}
{-# LANGUAGE RankNTypes #-}

-- |
-- Module       : Utxorpc.Types
-- Description  : Record types and type aliases.
-- The types in this module are required to call methods of a `UtxorpcClient`.
module Utxorpc.Types
  ( UtxorpcClient (..),
    BuildClient (..),
    SubmitClient (..),
    SyncClient (..),
    WatchClient (..),
    ServerStreamCall,
    ServerStreamReply,
    UnaryReply,
  )
where

import Network.GRPC.Client (HeaderList, RawReply)
import Proto.Utxorpc.V1alpha.Build.Build
import Proto.Utxorpc.V1alpha.Submit.Submit
import Proto.Utxorpc.V1alpha.Sync.Sync
import Proto.Utxorpc.V1alpha.Watch.Watch
import "http2-client" Network.HTTP2.Client (ClientError, TooMuchConcurrency)

-- | Type definition for functions that make calls to server stream methods.
-- Note that the stream state, a, can be different for each call.
type ServerStreamCall i o =
  forall a.
  -- | The initial state for the stream processing function.
  a ->
  -- | The request message to send to the service.
  i ->
  -- | The stream processing function. It is a fold over some state a with stream messages o.
  (a -> HeaderList -> o -> IO a) ->
  -- | The final state of the stream processing function, or an error.
  ServerStreamReply a

-- | The type returned by calls to unary service methods.
type UnaryReply o =
  IO
    (Either ClientError (Either TooMuchConcurrency (RawReply o)))

-- | The type returned by calls to server stream methods. a is the final state of the stream processing function.
type ServerStreamReply a =
  IO
    (Either ClientError (Either TooMuchConcurrency (a, HeaderList, HeaderList)))

{---------------------------------------
  UtxorpcClient
---------------------------------------}

-- | Methods for each module in UTxO RPC.
--
-- >>> fetchBlock (buildClient client) defMessage
data UtxorpcClient = UtxorpcClient
  { -- | Build module service methods.
    UtxorpcClient -> BuildClient
buildClient :: BuildClient,
    -- | Submit module service methods.
    UtxorpcClient -> SubmitClient
submitClient :: SubmitClient,
    -- | Sync module service methods.
    UtxorpcClient -> SyncClient
syncClient :: SyncClient,
    -- | Watch module service methods.
    UtxorpcClient -> WatchClient
watchClient :: WatchClient,
    -- | Closes the gRPC connection.
    UtxorpcClient -> IO (Either ClientError ())
close :: IO (Either ClientError ())
  }

{---------------------------------------
  Build
---------------------------------------}

-- | Methods of the Build module
data BuildClient = BuildClient
  { BuildClient -> GetChainTipRequest -> UnaryReply GetChainTipResponse
getChainTip :: GetChainTipRequest -> UnaryReply GetChainTipResponse,
    BuildClient
-> GetChainParamRequest -> UnaryReply GetChainParamResponse
getChainParam :: GetChainParamRequest -> UnaryReply GetChainParamResponse,
    BuildClient
-> GetUtxoByAddressRequest -> UnaryReply GetUtxoByAddressResponse
getUtxoByAddress :: GetUtxoByAddressRequest -> UnaryReply GetUtxoByAddressResponse,
    BuildClient
-> GetUtxoByRefRequest -> UnaryReply GetUtxoByRefResponse
getUtxoByRef :: GetUtxoByRefRequest -> UnaryReply GetUtxoByRefResponse,
    BuildClient -> ServerStreamCall HoldUtxoRequest HoldUtxoResponse
holdUtxo :: ServerStreamCall HoldUtxoRequest HoldUtxoResponse
  }

{---------------------------------------
  Submit
---------------------------------------}

-- | Methods of the Submit module
data SubmitClient = SubmitClient
  { SubmitClient -> SubmitTxRequest -> UnaryReply SubmitTxResponse
submitTx :: SubmitTxRequest -> UnaryReply SubmitTxResponse,
    SubmitClient
-> ReadMempoolRequest -> UnaryReply ReadMempoolResponse
readMempool :: ReadMempoolRequest -> UnaryReply ReadMempoolResponse,
    SubmitClient -> ServerStreamCall WaitForTxRequest WaitForTxResponse
waitForTx :: ServerStreamCall WaitForTxRequest WaitForTxResponse,
    SubmitClient
-> ServerStreamCall WatchMempoolRequest WatchMempoolResponse
watchMempool :: ServerStreamCall WatchMempoolRequest WatchMempoolResponse
  }

{---------------------------------------
  Sync
---------------------------------------}

-- | Methods of the Sync module
data SyncClient = SyncClient
  { SyncClient -> FetchBlockRequest -> UnaryReply FetchBlockResponse
fetchBlock :: FetchBlockRequest -> UnaryReply FetchBlockResponse,
    SyncClient -> DumpHistoryRequest -> UnaryReply DumpHistoryResponse
dumpHistory :: DumpHistoryRequest -> UnaryReply DumpHistoryResponse,
    SyncClient -> ServerStreamCall FollowTipRequest FollowTipResponse
followTip :: ServerStreamCall FollowTipRequest FollowTipResponse
  }

{---------------------------------------
  Watch
---------------------------------------}

-- | Methods of the watch module
newtype WatchClient = WatchClient
  { WatchClient -> ServerStreamCall WatchTxRequest WatchTxResponse
watchTx :: ServerStreamCall WatchTxRequest WatchTxResponse
  }