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/codec"
	codectypes "git.cw.tr/mukan-network/mukan-sdk/codec/types"
	cryptocodec "git.cw.tr/mukan-network/mukan-sdk/crypto/codec"
	"git.cw.tr/mukan-network/mukan-sdk/types/module"
	simtypes "git.cw.tr/mukan-network/mukan-sdk/types/simulation"

	genesistypes "git.cw.tr/mukan-network/mukan-ibc/modules/apps/27-interchain-accounts/genesis/types"
	"git.cw.tr/mukan-network/mukan-ibc/modules/apps/27-interchain-accounts/simulation"
	"git.cw.tr/mukan-network/mukan-ibc/modules/apps/27-interchain-accounts/types"
)

// TestRandomizedGenState tests the normal scenario of applying RandomizedGenState.
// Abonormal scenarios are not tested here.
func TestRandomizedGenState(t *testing.T) {
	interfaceRegistry := codectypes.NewInterfaceRegistry()
	cryptocodec.RegisterInterfaces(interfaceRegistry)
	cdc := codec.NewProtoCodec(interfaceRegistry)

	s := rand.NewSource(1) // 1 is the seed
	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 icaGenesis genesistypes.GenesisState
	simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &icaGenesis)

	require.True(t, icaGenesis.ControllerGenesisState.Params.ControllerEnabled)
	require.Empty(t, icaGenesis.ControllerGenesisState.ActiveChannels)
	require.Empty(t, icaGenesis.ControllerGenesisState.InterchainAccounts)
	require.Empty(t, icaGenesis.ControllerGenesisState.Ports)

	require.True(t, icaGenesis.HostGenesisState.Params.HostEnabled)
	require.Equal(t, []string{"*"}, icaGenesis.HostGenesisState.Params.AllowMessages)
	require.Equal(t, types.HostPortID, icaGenesis.HostGenesisState.Port)
	require.Empty(t, icaGenesis.ControllerGenesisState.ActiveChannels)
	require.Empty(t, icaGenesis.ControllerGenesisState.InterchainAccounts)
}
