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

-- | A constitution's off-chain anchor and optional guardrails script hash.
-- Assembly is total: @Constitution anchor Nothing@ has no guardrails script.
module Cometa.Governance.Constitution
  ( Constitution (..),
    encodeCbor,
    decodeCbor,
  )
where

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

import qualified Codec.CBOR.Decoding as D
import qualified Codec.CBOR.Encoding as E
import Cometa.Credential (ScriptHash)
import qualified Cometa.Credential as C
import Cometa.Error (Error)
import Cometa.Governance.Anchor (Anchor)
import qualified Cometa.Governance.Anchor as Anchor
import qualified Cometa.Internal.ProposalCbor as Cbor
import Data.ByteString (ByteString)

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

-- | Immutable metadata and an optional exact 28-byte guardrails hash.
data Constitution = Constitution
  { -- | Off-chain constitution location and expected payload hash.
    Constitution -> Anchor
constitutionAnchor :: !Anchor,
    -- | Optional guardrails script hash; absence encodes as null.
    Constitution -> Maybe ScriptHash
constitutionScript :: !(Maybe ScriptHash)
  }
  deriving (Constitution -> Constitution -> Bool
(Constitution -> Constitution -> Bool)
-> (Constitution -> Constitution -> Bool) -> Eq Constitution
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Constitution -> Constitution -> Bool
== :: Constitution -> Constitution -> Bool
$c/= :: Constitution -> Constitution -> Bool
/= :: Constitution -> Constitution -> Bool
Eq, Eq Constitution
Eq Constitution =>
(Constitution -> Constitution -> Ordering)
-> (Constitution -> Constitution -> Bool)
-> (Constitution -> Constitution -> Bool)
-> (Constitution -> Constitution -> Bool)
-> (Constitution -> Constitution -> Bool)
-> (Constitution -> Constitution -> Constitution)
-> (Constitution -> Constitution -> Constitution)
-> Ord Constitution
Constitution -> Constitution -> Bool
Constitution -> Constitution -> Ordering
Constitution -> Constitution -> Constitution
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Constitution -> Constitution -> Ordering
compare :: Constitution -> Constitution -> Ordering
$c< :: Constitution -> Constitution -> Bool
< :: Constitution -> Constitution -> Bool
$c<= :: Constitution -> Constitution -> Bool
<= :: Constitution -> Constitution -> Bool
$c> :: Constitution -> Constitution -> Bool
> :: Constitution -> Constitution -> Bool
$c>= :: Constitution -> Constitution -> Bool
>= :: Constitution -> Constitution -> Bool
$cmax :: Constitution -> Constitution -> Constitution
max :: Constitution -> Constitution -> Constitution
$cmin :: Constitution -> Constitution -> Constitution
min :: Constitution -> Constitution -> Constitution
Ord, Int -> Constitution -> ShowS
[Constitution] -> ShowS
Constitution -> String
(Int -> Constitution -> ShowS)
-> (Constitution -> String)
-> ([Constitution] -> ShowS)
-> Show Constitution
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Constitution -> ShowS
showsPrec :: Int -> Constitution -> ShowS
$cshow :: Constitution -> String
show :: Constitution -> String
$cshowList :: [Constitution] -> ShowS
showList :: [Constitution] -> ShowS
Show)

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

-- | Fresh definite CBOR; absence of a guardrails script encodes as null.
encodeCbor :: Constitution -> ByteString
encodeCbor :: Constitution -> ByteString
encodeCbor (Constitution Anchor
anchor Maybe ScriptHash
script) =
  Encoding -> ByteString
Cbor.encode
    ( Word -> Encoding
E.encodeListLen Word
2
        Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Cbor.raw (Anchor -> ByteString
Anchor.encodeCbor Anchor
anchor)
        Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (ScriptHash -> Encoding) -> Maybe ScriptHash -> Encoding
forall a. (a -> Encoding) -> Maybe a -> Encoding
Cbor.maybeEncoding (ByteString -> Encoding
E.encodeBytes (ByteString -> Encoding)
-> (ScriptHash -> ByteString) -> ScriptHash -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScriptHash -> ByteString
C.scriptHashBytes) Maybe ScriptHash
script
    )

-- | Decode one complete constitution; invalid schema or trailing bytes returns
-- 'Cometa.Error.InvalidProposalCbor'. This returns semantic data.
decodeCbor :: ByteString -> Either Error Constitution
decodeCbor :: ByteString -> Either Error Constitution
decodeCbor =
  (forall s. Decoder s Constitution)
-> ByteString -> Either Error Constitution
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
Cbor.decode
    ( Int -> Decoder s Constitution -> Decoder s Constitution
forall s a. Int -> Decoder s a -> Decoder s a
Cbor.array
        Int
2
        ( Anchor -> Maybe ScriptHash -> Constitution
Constitution
            (Anchor -> Maybe ScriptHash -> Constitution)
-> Decoder s Anchor -> Decoder s (Maybe ScriptHash -> Constitution)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ByteString -> Either Error Anchor) -> Decoder s Anchor
forall a s. (ByteString -> Either Error a) -> Decoder s a
Cbor.nested ByteString -> Either Error Anchor
Anchor.decodeCbor
            Decoder s (Maybe ScriptHash -> Constitution)
-> Decoder s (Maybe ScriptHash) -> Decoder s Constitution
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
<*> Decoder s ScriptHash -> Decoder s (Maybe ScriptHash)
forall s a. Decoder s a -> Decoder s (Maybe a)
Cbor.optional (Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes Decoder s ByteString
-> (ByteString -> Decoder s ScriptHash) -> Decoder s ScriptHash
forall a b. Decoder s a -> (a -> Decoder s b) -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Error -> Decoder s ScriptHash)
-> (ScriptHash -> Decoder s ScriptHash)
-> Either Error ScriptHash
-> Decoder s ScriptHash
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s ScriptHash
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s ScriptHash)
-> (Error -> String) -> Error -> Decoder s ScriptHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) ScriptHash -> Decoder s ScriptHash
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error ScriptHash -> Decoder s ScriptHash)
-> (ByteString -> Either Error ScriptHash)
-> ByteString
-> Decoder s ScriptHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Error ScriptHash
C.scriptHashFromBytes)
        )
    )