-- Copyright 2026 Biglup Labs
-- SPDX-License-Identifier: Apache-2.0
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
{-# LANGUAGE OverloadedStrings #-}

-- | Inject blockchain queries and explicit transaction effects as ordinary IO
-- functions. Missing values use Maybe, successful empty queries use empty lists,
-- and expected failures use Either. These independent observations do not imply
-- an atomic ledger snapshot. Operations never submit a transaction implicitly.
--
-- @
-- fake = (unavailable "Offline fixture" 42) {utxosAt = \_ -> pure (Right [])}
-- @
module Cometa.Provider (Provider (..), unavailable) where

-- IMPORTS --------------------------------------------------------------------

import Cometa.Address (Address)
import Cometa.Address.Reward (RewardAddress)
import Cometa.AssetId (AssetId)
import Cometa.Error (Error (..))
import Cometa.Evaluation (Budgets)
import Cometa.PlutusData (EncodedPlutusData)
import Cometa.Protocol.Parameters (Parameters)
import Cometa.Transaction (Transaction)
import Cometa.Transaction.Datum (Hash32)
import Cometa.Transaction.Input (Input)
import Cometa.Transaction.Utxo (Utxo)
import Cometa.TransactionId (TransactionId)
import Data.Text (Text)
import Data.Word (Word32, Word64)

-- TYPES ----------------------------------------------------------------------

-- | An application-supplied backend. Fields may perform network effects, use a
-- local cache or return deterministic fixtures. Exceptions propagate normally;
-- expected backend failures belong in Left. No equality or Show instance is
-- defined for function-valued capabilities.
data Provider = Provider
  { -- | Diagnostic name. When used in Cometa operations, supply at most 255
    -- UTF-8 bytes and no NUL character. The name is not a credential or endpoint URL.
    Provider -> Text
name :: !Text,
    -- | Network magic, distinct from a four-bit address network identifier.
    Provider -> Word32
networkMagic :: !Word32,
    -- | Complete protocol snapshot; optional era fields retain availability.
    Provider -> IO (Either Error Parameters)
protocolParameters :: IO (Either Error Parameters),
    -- | Unspent outputs at an address. Empty is a successful no-match query.
    Provider -> Address -> IO (Either Error [Utxo])
utxosAt :: Address -> IO (Either Error [Utxo]),
    -- | Current reward balance, in lovelace from zero through 2^64 - 1.
    Provider -> RewardAddress -> IO (Either Error Word64)
rewardBalance :: RewardAddress -> IO (Either Error Word64),
    -- | Unspent outputs at the address containing the specified asset.
    Provider -> Address -> AssetId -> IO (Either Error [Utxo])
utxosWithAsset :: Address -> AssetId -> IO (Either Error [Utxo]),
    -- | The unique output containing an asset assumed to be an NFT. Nothing
    -- means absent; ambiguous matches must return an expected backend failure.
    Provider -> AssetId -> IO (Either Error (Maybe Utxo))
utxoWithAsset :: AssetId -> IO (Either Error (Maybe Utxo)),
    -- | Resolve references. Missing inputs are omitted, not turned into errors;
    -- callers needing complete resolution must check every requested reference.
    Provider -> [Input] -> IO (Either Error [Utxo])
resolveInputs :: [Input] -> IO (Either Error [Utxo]),
    -- | Resolve exact datum bytes. Nothing means the datum is unavailable.
    Provider -> Hash32 -> IO (Either Error (Maybe EncodedPlutusData))
resolveDatum :: Hash32 -> IO (Either Error (Maybe EncodedPlutusData)),
    -- | Wait up to the supplied timeout in milliseconds. False means not
    -- confirmed within that interval; transport failures remain Left.
    Provider -> TransactionId -> Word64 -> IO (Either Error Bool)
awaitTransaction :: TransactionId -> Word64 -> IO (Either Error Bool),
    -- | Explicitly submit exact transaction bytes and return the accepted ID.
    -- A successful result does not claim confirmation or finality.
    Provider -> Transaction -> IO (Either Error TransactionId)
submitTransaction :: Transaction -> IO (Either Error TransactionId),
    -- | Evaluate top-level redeemers with additional resolved UTxOs. Return
    -- budgets for every existing top-level purpose/index without changing input.
    -- No partial-success or evaluation-log information is implied.
    Provider -> Transaction -> [Utxo] -> IO (Either Error Budgets)
evaluateTransaction :: Transaction -> [Utxo] -> IO (Either Error Budgets)
  }

-- DEFINITIONS ----------------------------------------------------------------

-- | Total default backend with explicit metadata. Override only the capabilities
-- required by a workflow. Every operation initially returns UnsupportedOperation
-- naming that operation; none acquires a connection or performs network work.
unavailable :: Text -> Word32 -> Provider
unavailable :: Text -> Word32 -> Provider
unavailable Text
backendName Word32
magic =
  Text
-> Word32
-> IO (Either Error Parameters)
-> (Address -> IO (Either Error [Utxo]))
-> (RewardAddress -> IO (Either Error Word64))
-> (Address -> AssetId -> IO (Either Error [Utxo]))
-> (AssetId -> IO (Either Error (Maybe Utxo)))
-> ([Input] -> IO (Either Error [Utxo]))
-> (Hash32 -> IO (Either Error (Maybe EncodedPlutusData)))
-> (TransactionId -> Word64 -> IO (Either Error Bool))
-> (Transaction -> IO (Either Error TransactionId))
-> (Transaction -> [Utxo] -> IO (Either Error Budgets))
-> Provider
Provider
    Text
backendName
    Word32
magic
    (Text -> IO (Either Error Parameters)
forall {f :: * -> *} {b}.
Applicative f =>
Text -> f (Either Error b)
unsupported Text
"provider.protocolParameters")
    (\Address
_ -> Text -> IO (Either Error [Utxo])
forall {f :: * -> *} {b}.
Applicative f =>
Text -> f (Either Error b)
unsupported Text
"provider.utxosAt")
    (\RewardAddress
_ -> Text -> IO (Either Error Word64)
forall {f :: * -> *} {b}.
Applicative f =>
Text -> f (Either Error b)
unsupported Text
"provider.rewardBalance")
    (\Address
_ AssetId
_ -> Text -> IO (Either Error [Utxo])
forall {f :: * -> *} {b}.
Applicative f =>
Text -> f (Either Error b)
unsupported Text
"provider.utxosWithAsset")
    (\AssetId
_ -> Text -> IO (Either Error (Maybe Utxo))
forall {f :: * -> *} {b}.
Applicative f =>
Text -> f (Either Error b)
unsupported Text
"provider.utxoWithAsset")
    (\[Input]
_ -> Text -> IO (Either Error [Utxo])
forall {f :: * -> *} {b}.
Applicative f =>
Text -> f (Either Error b)
unsupported Text
"provider.resolveInputs")
    (\Hash32
_ -> Text -> IO (Either Error (Maybe EncodedPlutusData))
forall {f :: * -> *} {b}.
Applicative f =>
Text -> f (Either Error b)
unsupported Text
"provider.resolveDatum")
    (\TransactionId
_ Word64
_ -> Text -> IO (Either Error Bool)
forall {f :: * -> *} {b}.
Applicative f =>
Text -> f (Either Error b)
unsupported Text
"provider.awaitTransaction")
    (\Transaction
_ -> Text -> IO (Either Error TransactionId)
forall {f :: * -> *} {b}.
Applicative f =>
Text -> f (Either Error b)
unsupported Text
"provider.submitTransaction")
    (\Transaction
_ [Utxo]
_ -> Text -> IO (Either Error Budgets)
forall {f :: * -> *} {b}.
Applicative f =>
Text -> f (Either Error b)
unsupported Text
"provider.evaluateTransaction")
  where
    unsupported :: Text -> f (Either Error b)
unsupported Text
operation = Either Error b -> f (Either Error b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error b
forall a b. a -> Either a b
Left (Text -> Error
UnsupportedOperation Text
operation))