package simulation_test

import (
	"encoding/json"
	"math/rand"
	"testing"

	"github.com/stretchr/testify/require"

	sdkmath "cosmossdk.io/math"

	"git.cw.tr/mukan-network/mukan-sdk/types/module"
	moduletestutil "git.cw.tr/mukan-network/mukan-sdk/types/module/testutil"
	simtypes "git.cw.tr/mukan-network/mukan-sdk/types/simulation"
	"git.cw.tr/mukan-network/mukan-sdk/x/bank"
	"git.cw.tr/mukan-network/mukan-sdk/x/group"
	groupmodule "git.cw.tr/mukan-network/mukan-sdk/x/group/module"
	"git.cw.tr/mukan-network/mukan-sdk/x/group/simulation"
)

func TestRandomizedGenState(t *testing.T) {
	encodingConfig := moduletestutil.MakeTestEncodingConfig(groupmodule.AppModuleBasic{}, bank.AppModuleBasic{})
	cdc := encodingConfig.Codec

	s := rand.NewSource(1)
	r := rand.New(s)

	simState := module.SimulationState{
		AppParams:    make(simtypes.AppParams),
		Cdc:          cdc,
		Rand:         r,
		NumBonded:    3,
		Accounts:     simtypes.RandomAccounts(r, 3),
		InitialStake: sdkmath.NewInt(1000),
		GenState:     make(map[string]json.RawMessage),
	}

	simulation.RandomizedGenState(&simState)
	var groupGenesis group.GenesisState
	simState.Cdc.MustUnmarshalJSON(simState.GenState[group.ModuleName], &groupGenesis)

	require.Equal(t, int(groupGenesis.GroupSeq), len(simState.Accounts))
	require.Len(t, groupGenesis.Groups, len(simState.Accounts))
	require.Len(t, groupGenesis.GroupMembers, len(simState.Accounts))
	require.Equal(t, int(groupGenesis.GroupPolicySeq), len(simState.Accounts))
	require.Len(t, groupGenesis.GroupPolicies, len(simState.Accounts))
	require.Equal(t, int(groupGenesis.ProposalSeq), len(simState.Accounts))
	require.Len(t, groupGenesis.Proposals, len(simState.Accounts))
	require.Len(t, groupGenesis.Votes, len(simState.Accounts))
}
