-- 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.

-- | An unspent-output snapshot pairs a reference with its resolved output.
-- Construction does not query chain state or prove that the output is unspent.
--
-- @
-- resolved reference output = utxo reference output
-- @
module Cometa.Transaction.Utxo (Utxo, utxo, input, output, encodeCbor, decodeCbor) where

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

import qualified Codec.CBOR.Encoding as E
import Cometa.Error (Error)
import qualified Cometa.Internal.TransactionCbor as Cbor
import Cometa.Transaction.Input (Input)
import qualified Cometa.Transaction.Input as Input
import Cometa.Transaction.Output (Output)
import qualified Cometa.Transaction.Output as Output
import Data.ByteString (ByteString)

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

-- | Owned resolved reference and semantic output. Equality compares both fields.
data Utxo = Utxo !Input !Output deriving (Utxo -> Utxo -> Bool
(Utxo -> Utxo -> Bool) -> (Utxo -> Utxo -> Bool) -> Eq Utxo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Utxo -> Utxo -> Bool
== :: Utxo -> Utxo -> Bool
$c/= :: Utxo -> Utxo -> Bool
/= :: Utxo -> Utxo -> Bool
Eq, Int -> Utxo -> ShowS
[Utxo] -> ShowS
Utxo -> String
(Int -> Utxo -> ShowS)
-> (Utxo -> String) -> ([Utxo] -> ShowS) -> Show Utxo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Utxo -> ShowS
showsPrec :: Int -> Utxo -> ShowS
$cshow :: Utxo -> String
show :: Utxo -> String
$cshowList :: [Utxo] -> ShowS
showList :: [Utxo] -> ShowS
Show)

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

-- | Assemble a resolved reference and its output without chain-state validation.
utxo :: Input -> Output -> Utxo
utxo :: Input -> Output -> Utxo
utxo = Input -> Output -> Utxo
Utxo

-- | Reference identifying the resolved output.
input :: Utxo -> Input
input :: Utxo -> Input
input (Utxo Input
result Output
_) = Input
result

-- | Resolved semantic output; inspecting it has no chain or native effects.
output :: Utxo -> Output
output :: Utxo -> Output
output (Utxo Input
_ Output
result) = Output
result

-- | Fresh native UTxO pair @[input, output]@. This is a snapshot interchange
-- encoding, not an on-chain transaction-body field.
encodeCbor :: Utxo -> ByteString
encodeCbor :: Utxo -> ByteString
encodeCbor (Utxo Input
reference Output
resolved) = Encoding -> ByteString
Cbor.encode (Word -> Encoding
E.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Cbor.raw (Input -> ByteString
Input.encodeCbor Input
reference) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Cbor.raw (Output -> ByteString
Output.encodeCbor Output
resolved))

-- | Decode a complete definite or indefinite pair through the public input and
-- output schemas. Malformed data and trailing bytes return structured errors.
-- This snapshot API inspects semantic outputs; use an enclosing exact transaction
-- or 'Output.EncodedOutput' when preserving signed-body identity is required.
decodeCbor :: ByteString -> Either Error Utxo
decodeCbor :: ByteString -> Either Error Utxo
decodeCbor ByteString
bytes = (forall s. Decoder s Utxo) -> ByteString -> Either Error Utxo
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
Cbor.decode (Int -> Decoder s Utxo -> Decoder s Utxo
forall s a. Int -> Decoder s a -> Decoder s a
Cbor.array Int
2 (Input -> Output -> Utxo
Utxo (Input -> Output -> Utxo)
-> Decoder s Input -> Decoder s (Output -> Utxo)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> (ByteString -> Either Error Input) -> Decoder s Input
forall a s.
ByteString -> (ByteString -> Either Error a) -> Decoder s a
Cbor.nested ByteString
bytes ByteString -> Either Error Input
Input.decodeCbor Decoder s (Output -> Utxo) -> Decoder s Output -> Decoder s Utxo
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (EncodedOutput -> Output
Output.toOutput (EncodedOutput -> Output)
-> Decoder s EncodedOutput -> Decoder s Output
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString
-> (ByteString -> Either Error EncodedOutput)
-> Decoder s EncodedOutput
forall a s.
ByteString -> (ByteString -> Either Error a) -> Decoder s a
Cbor.nested ByteString
bytes ByteString -> Either Error EncodedOutput
Output.decodeCbor))) ByteString
bytes