// createPaymentProofs provides functions to create payment proofs

package createPaymentProofs

//TODO: Restrict payment proofs to not allow moderator identities or non-mate profiles to be funded

import "seekia/internal/encoding"
import "seekia/internal/helpers"

import messagepack "github.com/vmihailenco/msgpack/v5"

import "errors"

// Inputs:
//	-byte: The network type the proof belongs to
//		-1 == Mainnet, 2 == Testnet
//	-string: The cryptocurrency this payment proof belongs to
//		-Example: "Ethereum"
//	-[]byte: Content or identity hash that the proof is paying for
//	-[][32]byte: A list of nodes that makes up the tree path of the proof
//		-Each [32]byte value is a node in the tree
//	Outputs:
//	-[]byte: Payment proof (encoded in MessagePack)
//	-error
func CreatePaymentProof(networkType byte, cryptocurrencyName string, contentOrIdentityHash []byte, treePath [][32]byte)([]byte, error){

	isValid := helpers.VerifyNetworkType(networkType)
	if (isValid == false){
		networkTypeString := helpers.ConvertByteToString(networkType)
		return nil, errors.New("CreatePaymentProof called with invalid networkType: " + networkTypeString)
	}

	if (cryptocurrencyName != "Ethereum"){
		return nil, errors.New("CreatePaymentProof called with invalid cryptocurrencyName: " + cryptocurrencyName)
	}

	cryptocurrencyByte := byte(1)

	contentOrIdentityHashLength := len(contentOrIdentityHash)

	if (contentOrIdentityHashLength != 16 && contentOrIdentityHashLength != 26 && contentOrIdentityHashLength != 28 && contentOrIdentityHashLength != 30){
		contentOrIdentityHashHex := encoding.EncodeBytesToHexString(contentOrIdentityHash)
		return nil, errors.New("CreatePaymentProof called with invalid contentOrIdentityHash: " + contentOrIdentityHashHex)
	}

	if (treePath == nil){
		return nil, errors.New("CreatePaymentProof called with nil treePath.")
	}

	proofVersionEncoded, err := encoding.EncodeMessagePackBytes(1)
	if (err != nil) { return nil, err }

	networkTypeEncoded, err := encoding.EncodeMessagePackBytes(networkType)
	if (err != nil) { return nil, err }

	cryptocurrencyByteEncoded, err := encoding.EncodeMessagePackBytes(cryptocurrencyByte)
	if (err != nil) { return nil, err }

	contentOrIdentityHashEncoded, err := encoding.EncodeMessagePackBytes(contentOrIdentityHash)
	if (err != nil) { return nil, err }

	treePathEncoded, err := encoding.EncodeMessagePackBytes(treePath)
	if (err != nil) { return nil, err }

	paymentProofEncodedSlice := []messagepack.RawMessage{proofVersionEncoded, networkTypeEncoded, cryptocurrencyByteEncoded, contentOrIdentityHashEncoded, treePathEncoded}

	paymentProofBytes, err := encoding.EncodeMessagePackBytes(paymentProofEncodedSlice)
	if (err != nil) { return nil, err }

	return paymentProofBytes, nil
}

