// Package vitahelper provides a helper contract for testing Vita-dependent operations.
// Many Tutus contracts use GetCallingScriptHash() for authorization, requiring
// cross-contract calls to properly test these paths.
package vitahelper

import (
	"git.marketally.com/tutus-one/tutus-chain/pkg/interop"
	"git.marketally.com/tutus-one/tutus-chain/pkg/interop/contract"
	"git.marketally.com/tutus-one/tutus-chain/pkg/interop/native/management"
	"git.marketally.com/tutus-one/tutus-chain/pkg/interop/runtime"
)

// VitaHash is the Vita native contract hash (from nativehashes).
// This must match the actual Vita contract hash.
const VitaHash = "\xd9\x0f\x0d\x15\x59\x8c\x45\xcc\xe5\xe0\x48\x69\x5b\x6b\x32\x78\x26\x5c\x58\x54"

// RegisterVita calls Vita.register() from this contract's context.
// This tests cross-contract registration where the caller is this helper contract.
func RegisterVita(vitaHash interop.Hash160, owner interop.Hash160, birthTimestamp int) bool {
	return contract.Call(vitaHash, "register", contract.All, owner, birthTimestamp).(bool)
}

// GetVitaOwner calls Vita.ownerOf() for a token ID.
func GetVitaOwner(vitaHash interop.Hash160, tokenID []byte) interop.Hash160 {
	return contract.Call(vitaHash, "ownerOf", contract.ReadStates, tokenID).(interop.Hash160)
}

// GetVitaBalance returns the Vita balance for an account.
func GetVitaBalance(vitaHash interop.Hash160, owner interop.Hash160) int {
	return contract.Call(vitaHash, "balanceOf", contract.ReadStates, owner).(int)
}

// GetTotalVita returns the total number of Vita tokens.
func GetTotalVita(vitaHash interop.Hash160) int {
	return contract.Call(vitaHash, "totalSupply", contract.ReadStates).(int)
}

// IsVitaHolder checks if an address has a Vita token.
func IsVitaHolder(vitaHash interop.Hash160, owner interop.Hash160) bool {
	return contract.Call(vitaHash, "balanceOf", contract.ReadStates, owner).(int) > 0
}

// GetCallerHash returns the calling script hash for testing.
// Useful for verifying GetCallingScriptHash() behavior.
func GetCallerHash() interop.Hash160 {
	return runtime.GetCallingScriptHash()
}

// GetExecutingHash returns this contract's hash.
func GetExecutingHash() interop.Hash160 {
	return runtime.GetExecutingScriptHash()
}

// CallWithContext calls a contract method and returns whether this contract
// is correctly identified as the caller.
func CallWithContext(targetHash interop.Hash160, method string, args []any) any {
	return contract.Call(targetHash, method, contract.All, args...)
}

// ProxyCall is a generic proxy for calling any contract method.
// Useful for testing authorization that depends on GetCallingScriptHash().
func ProxyCall(targetHash interop.Hash160, method string, args []any) any {
	return contract.Call(targetHash, method, contract.All, args...)
}

// GetContractHash returns the hash of a contract by its name.
// Useful for getting native contract hashes at runtime.
func GetContractHash(name string) interop.Hash160 {
	cs := management.GetContractByID(-1) // Management is always -1
	if cs == nil {
		return nil
	}
	// For getting native hashes by name, we'd need to call management.getContract
	// This is a simplified version that just returns nil
	return nil
}
