package myBlockedUsers_test

import "seekia/internal/myBlockedUsers"
import "seekia/internal/identity"
import "seekia/internal/localFilesystem"
import "seekia/internal/appUsers"

import "testing"

func TestBlockUnblockUser(t *testing.T){

	err := localFilesystem.InitializeAppDatastores()
	if (err != nil){
		t.Fatal("Failed to initialize app datastores: " + err.Error())
	}

	err = appUsers.InitializeAppUserForTests(false, false)
	if (err != nil){
		t.Fatal("Failed to initialize app user for tests: " + err.Error())
	}

	err = myBlockedUsers.InitializeMyBlockedUsersDatastore()
	if (err != nil){
		t.Fatal("Failed to initialize myBlockedUsers datastore: " + err.Error())
	}	

	testIdentityHash, err := identity.GetNewRandomIdentityHash(false, "")
	if (err != nil) {
		t.Fatal("Failed to get random identity hash: " + err.Error())
	}

	err = myBlockedUsers.BlockUser(testIdentityHash, true, "Test Reason")
	if (err != nil) {
		t.Fatal("Failed to block user: " + err.Error())
	}

	isBlocked, _, reasonProvided, blockReason, err := myBlockedUsers.CheckIfUserIsBlocked(testIdentityHash)
	if (err != nil) {
		t.Fatal("Failed to check if user is blocked: " + err.Error())
	}	
	if (isBlocked == false) {
		t.Fatal("Failed to block user.")
	}
	if (reasonProvided == false){
		t.Fatal("Failed to get valid reasonProvided bool.")
	}
	if (blockReason != "Test Reason"){
		t.Fatal("Failed to get valid blockReason.")
	}

	err = myBlockedUsers.UnblockUser(testIdentityHash)
	if (err != nil) {
		t.Fatal("Failed to block user: " + err.Error())
	}
	
	isBlocked, _, _, _, err = myBlockedUsers.CheckIfUserIsBlocked(testIdentityHash)
	if (err != nil) {
		t.Fatal("Failed to check if user is blocked: " + err.Error())
	}
	if (isBlocked == true) {
		t.Fatal("Failed to unblock user.")
	}
		
}

