package calculatedAttributes_test

import "seekia/internal/profiles/calculatedAttributes"

import "seekia/internal/appUsers"
import "seekia/internal/generate"
import "seekia/internal/identity"
import "seekia/internal/localFilesystem"
import "seekia/internal/profiles/readProfiles"

import "testing"



func TestCalculatedAttributes(t *testing.T){

	err := localFilesystem.InitializeAppDatastores()
	if (err != nil){
		t.Fatal("InitializeAppDatastores failed: " + err.Error())
	}

	err = appUsers.InitializeAppUserForTests(true, true)
	if (err != nil) {
		t.Fatal("InitializeAppUserForTests failed: " + err.Error())
	}

	calculatedAttributesList := calculatedAttributes.GetCalculatedAttributesList()

	for i:=0; i<10; i++{

		identityPublicKey, identityPrivateKey, err := identity.GetNewRandomPublicPrivateIdentityKeys()
		if (err != nil){
			t.Fatal("GetNewRandomPublicPrivateIdentityKeys failed: " + err.Error())
			return
		}

		fakeProfileBytes, err := generate.GetFakeProfile("Mate", identityPublicKey, identityPrivateKey, 1)
		if (err != nil){
			t.Fatal("GetFakeProfile failed: " + err.Error())
			return
		}

		ableToRead, profileVersion, _, profileAuthor, _, _, rawProfileMap, err := readProfiles.ReadProfile(true, fakeProfileBytes)
		if (err != nil){
			t.Fatal("ReadProfile failed: " + err.Error())
			return
		}
		if (ableToRead == false){
			t.Fatal("ReadProfile cant read profile returned from GetFakeProfile.")
			return
		}

		profileType, err := identity.GetIdentityTypeFromIdentityHash(profileAuthor)
		if (err != nil){
			t.Fatal("ReadProfile returning invalid identityHash: " + err.Error())
			return
		}

		getProfileAttributesFunction := func(attributeName string)(bool, int, string, error){

			attributeExists, attributeValue, err := readProfiles.GetFormattedProfileAttributeFromRawProfileMap(rawProfileMap, attributeName)
			if (err != nil) { return false, 0, "", err }
			if (attributeExists == false){
				return false, profileVersion, "", nil
			}
			return true, profileVersion, attributeValue, nil
		}

		for _, attributeName := range calculatedAttributesList{

			if (profileType != "Moderator"){
				if (attributeName == "IdentityScore" || attributeName == "Controversy" || attributeName == "NumberOfReviews"){
					continue
				}
			}

			_, _, _, err := calculatedAttributes.GetAnyProfileAttributeIncludingCalculated(attributeName, getProfileAttributesFunction)
			if (err != nil) {
				t.Fatal("GetAnyProfileAttributeIncludingCalculated failed: " + err.Error())
				return
			}
		}
	}
}


