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

-- | Human-readable category labels for logs and error messages. Unlike 'Show',
-- these functions describe only the category and never include hashes, keys or
-- payloads. They use the English cardano-c diagnostic vocabulary, not CIP-116
-- JSON tags. Use the domain encoders for machine interchange.
module Cometa.Diagnostics (certificateKind, nativeScriptKind, vote, voterKind) where

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

import qualified Cometa.Certificate as Certificate
import qualified Cometa.Credential as Credential
import qualified Cometa.Governance.Voting as Voting
import qualified Cometa.Script.Native as Native
import Data.Text (Text)

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

-- | Certificate category, for example @"Certificate Type: Stake Registration"@.
-- Field contents and registration/deposit state do not affect the label.
certificateKind :: Certificate.Certificate -> Text
certificateKind :: Certificate -> Text
certificateKind Certificate
value = case Certificate
value of
  Certificate.StakeRegistration {} -> Text
"Certificate Type: Stake Registration"
  Certificate.StakeDeregistration {} -> Text
"Certificate Type: Stake Deregistration"
  Certificate.StakeDelegation {} -> Text
"Certificate Type: Stake Delegation"
  Certificate.PoolRegistration {} -> Text
"Certificate Type: Pool Registration"
  Certificate.PoolRetirement {} -> Text
"Certificate Type: Pool Retirement"
  Certificate.GenesisKeyDelegation {} -> Text
"Certificate Type: Genesis Key Delegation"
  Certificate.MoveInstantaneousRewards {} -> Text
"Certificate Type: Move Instantaneous Rewards"
  Certificate.Registration {} -> Text
"Certificate Type: Registration"
  Certificate.Unregistration {} -> Text
"Certificate Type: Unregistration"
  Certificate.VoteDelegation {} -> Text
"Certificate Type: Vote Delegation"
  Certificate.StakeVoteDelegation {} -> Text
"Certificate Type: Stake Vote Delegation"
  Certificate.StakeRegistrationDelegation {} -> Text
"Certificate Type: Stake Registration Delegation"
  Certificate.VoteRegistrationDelegation {} -> Text
"Certificate Type: Vote Registration Delegation"
  Certificate.StakeVoteRegistrationDelegation {} -> Text
"Certificate Type: Stake Vote Registration Delegation"
  Certificate.AuthorizeCommitteeHot {} -> Text
"Certificate Type: Auth Committee Hot"
  Certificate.ResignCommitteeCold {} -> Text
"Certificate Type: Resign Committee Cold"
  Certificate.RegisterDRep {} -> Text
"Certificate Type: DRep Registration"
  Certificate.UnregisterDRep {} -> Text
"Certificate Type: DRep Unregistration"
  Certificate.UpdateDRep {} -> Text
"Certificate Type: Update DRep"

-- | Native script condition, for example @"Native Script Type: Require All Of"@.
-- Does not evaluate the script or expose any child condition or credential.
nativeScriptKind :: Native.NativeScript -> Text
nativeScriptKind :: NativeScript -> Text
nativeScriptKind NativeScript
value = case NativeScript -> NativeScriptView
Native.view NativeScript
value of
  Native.RequireSignature {} -> Text
"Native Script Type: Require Pubkey"
  Native.RequireAll {} -> Text
"Native Script Type: Require All Of"
  Native.RequireAny {} -> Text
"Native Script Type: Require Any Of"
  Native.RequireAtLeast {} -> Text
"Native Script Type: Require N Of K"
  Native.InvalidBefore {} -> Text
"Native Script Type: Invalid Before"
  Native.InvalidAfter {} -> Text
"Native Script Type: Invalid After"
  Native.RequireGuard {} -> Text
"Native Script Type: Require Guard"

-- | Voting choice as @"Vote: No"@, @"Vote: Yes"@ or @"Vote: Abstain"@.
vote :: Voting.Vote -> Text
vote :: Vote -> Text
vote Vote
value = case Vote
value of
  Vote
Voting.No -> Text
"Vote: No"
  Vote
Voting.Yes -> Text
"Vote: Yes"
  Vote
Voting.Abstain -> Text
"Vote: Abstain"

-- | Voting authority kind, distinguishing committee and DRep key/script
-- credentials from stake pool keys. The credential hash is never included.
voterKind :: Voting.Voter -> Text
voterKind :: Voter -> Text
voterKind Voter
value = case Voter
value of
  Voting.CommitteeVoter (Credential.KeyCredential KeyHash CommitteeHot
_) -> Text
"Voter Type: Constitutional Committee Key Hash"
  Voting.CommitteeVoter (Credential.ScriptCredential ScriptHash
_) -> Text
"Voter Type: Constitutional Committee Script Hash"
  Voting.DRepVoter (Credential.KeyCredential KeyHash DRep
_) -> Text
"Voter Type: DRep Key Hash"
  Voting.DRepVoter (Credential.ScriptCredential ScriptHash
_) -> Text
"Voter Type: DRep Script Hash"
  Voting.StakePoolVoter KeyHash StakePool
_ -> Text
"Voter Type: Stake Pool Key Hash"