{-# 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 (..),
    QueryClient (..),
    SubmitClient (..),
    SyncClient (..),
    WatchClient (..),
    ServerStreamCall,
    ServerStreamReply,
    UnaryReply,
  )
where

import Network.GRPC.Client (HeaderList, RawReply)
import Proto.Utxorpc.V1alpha.Query.Query
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 (queryClient client) defMessage
data UtxorpcClient = UtxorpcClient
  { -- | Query module service methods.
    UtxorpcClient -> QueryClient
queryClient :: QueryClient,
    -- | 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 ())
  }

{---------------------------------------
  Query
---------------------------------------}

-- | Methods of the Query module
data QueryClient = QueryClient
  { QueryClient -> ReadParamsRequest -> UnaryReply ReadParamsResponse
readParams :: ReadParamsRequest -> UnaryReply ReadParamsResponse,
    QueryClient -> ReadUtxosRequest -> UnaryReply ReadUtxosResponse
readUtxos :: ReadUtxosRequest -> UnaryReply ReadUtxosResponse,
    QueryClient -> SearchUtxosRequest -> UnaryReply SearchUtxosResponse
searchUtxos :: SearchUtxosRequest -> UnaryReply SearchUtxosResponse,
    QueryClient -> ServerStreamCall ReadUtxosRequest ReadUtxosResponse
streamUtxos :: ServerStreamCall ReadUtxosRequest ReadUtxosResponse
  }

{---------------------------------------
  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
  }