
// moderatorScores provides functions to retrieve Moderator identity scores.
// A moderator's identity score determines a moderator's rank and ability to ban other moderators.
// It is a measure of how much money has been sent to their identity cryptocurrency address(es), represented as the value in gold at time sent.

package moderatorScores

//TODO: Build package
// Moderator scores are constructed from identity deposits.
// Identity deposits are queried from blockchain hosts, or from a locally hosted blockchain node.
// We will use the verifiedAddressDeposits package for cryptocurrencies for which the user is running their own blockchain node
// We will use the trustedAddressDeposits package for cryptocurrencies for which the user is not running their own blockchain node

// We calculate the score by multiplying each deposit amount by the gold exchange rate at the time of the deposit and summing all gold amounts
// The exchange rates are downloaded from the parameters, so all hosts and moderators will agree on everyone's score

// Number of reviews describes how many reviews this moderator can create
// This limit prevents moderators from spamming reviews
// It is calculated by multiplying the moderator score by a constant provided by the network parameters
// There should also be an upper limit for all moderators, regardless of their score

import "seekia/internal/badgerDatabase"
import "seekia/internal/cryptocurrency/cardanoAddress"
import "seekia/internal/cryptocurrency/ethereumAddress"
import "seekia/internal/encoding"
import "seekia/internal/identity"
import "seekia/internal/moderation/trustedAddressDeposits"
import "seekia/internal/moderation/verifiedAddressDeposits"

import "errors"

//Outputs:
//	-bool: Score is known (Deposits have been downloaded from hosts, and parameters exist)
//	-float64: Moderator score
//	-bool: Moderator score is sufficient (this is required for reviews to be hosted and to have an effect on the moderation consensus)
//	-bool: Moderator is able to ban other moderators
//	-int: Number of reviews allowed
//	-error
func GetModeratorIdentityScore(identityHash [16]byte)(bool, float64, bool, bool, int, error){

	isValid, err := identity.VerifyIdentityHash(identityHash, true, "Moderator")
	if (err != nil) { return false, 0, false, false, 0, err }
	if (isValid == false){
		identityHashHex := encoding.EncodeBytesToHexString(identityHash[:])
		return false, 0, false, false, 0, errors.New("GetModeratorIdentityScore called with invalid identityHash: " + identityHashHex)
	}

	//TODO

	return true, 100, true, true, 100, nil
}


//Outputs:
//	-bool: Local blockchain node is being hoste (all addresses are known)
//	-[]string: List of addresses whose address deposits (whose deposits we do not know, or we do know)
//	-error
func GetModeratorCryptoDepositAddressesList(cryptocurrency string, knownOrUnknown string)(bool, []string, error){
	
	if (cryptocurrency != "Ethereum" && cryptocurrency != "Cardano"){
		return false, nil, errors.New("GetModeratorCryptoAddressesList called with invalid cryptocurrency: " + cryptocurrency)
	}

	if (knownOrUnknown != "Known" && knownOrUnknown != "Unknown"){
		return false, nil, errors.New("GetModeratorCryptoAddressesList called with invalid knownOrUnknown: " + knownOrUnknown)
	}

	nodeIsHosted, err := verifiedAddressDeposits.CheckIfCryptocurrencyNodeIsBeingHosted(cryptocurrency)
	if (err != nil) { return false, nil, err }
	if (nodeIsHosted == true){
		return true, nil, nil
	}

	allModeratorIdentityHashesList, err := badgerDatabase.GetAllProfileIdentityHashes("Moderator")
	if (err != nil) { return false, nil, err }

	depositAddressesList := make([]string, 0)

	for _, moderatorIdentityHash := range allModeratorIdentityHashesList{

		getModeratorDepositAddress := func()(string, error){

			if (cryptocurrency == "Ethereum"){

				depositAddress, err := ethereumAddress.GetIdentityScoreEthereumAddressFromIdentityHash(moderatorIdentityHash)
				if (err != nil) { return "", err }

				return depositAddress, nil
			}

			// cryptocurrency == "Cardano"

			depositAddress, err := cardanoAddress.GetIdentityScoreCardanoAddressFromIdentityHash(moderatorIdentityHash)
			if (err != nil) { return "", err }

			return depositAddress, nil
		}

		depositAddress, err := getModeratorDepositAddress()
		if (err != nil) { return false, nil, err }

		depositsAreKnown, err := trustedAddressDeposits.CheckIfAddressDepositsAreKnown(cryptocurrency, depositAddress)
		if (err != nil) { return false, nil, err }
		if (depositsAreKnown == false){
			if (knownOrUnknown == "Unknown"){
				depositAddressesList = append(depositAddressesList, depositAddress)
			}		
		} else {
			if (knownOrUnknown == "Known"){
				depositAddressesList = append(depositAddressesList, depositAddress)
			}
		}
	}

	return false, depositAddressesList, nil
}


