package app

import (
	"fmt"
	"testing"

	"cosmossdk.io/log"
	wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
	tmdb "github.com/cosmos/cosmos-db"
	sdk "github.com/cosmos/cosmos-sdk/types"
	"github.com/stretchr/testify/require"
)

// import (
// 	"fmt"
// 	"testing"
// 	"time"

// 	"cosmossdk.io/log"
// 	math "cosmossdk.io/math"
// 	tmjson "github.com/cometbft/cometbft/libs/json"
// 	tmtypes "github.com/cometbft/cometbft/types"
// 	tmdb "github.com/cosmos/cosmos-db"
// 	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
// 	cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
// 	"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
// 	"github.com/cosmos/cosmos-sdk/testutil/mock"
// 	simapp "github.com/cosmos/cosmos-sdk/testutil/sims"
// 	sdk "github.com/cosmos/cosmos-sdk/types"
// 	authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
// 	banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
// 	stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
// 	"github.com/stretchr/testify/require"

// 	abci "github.com/cometbft/cometbft/abci/types"

// 	wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
// )

var emptyWasmOpts []wasmkeeper.Option = nil

// func TestArchwaydExport(t *testing.T) {
// 	db := tmdb.NewMemDB()
// 	logger := log.NewNopLogger()
// 	encoding := MakeEncodingConfig()
// 	gapp := NewArchwayApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encoding,
// 		EmptyBaseAppOptions{}, emptyWasmOpts)

// 	privValidator := mock.NewPV()
// 	pubKey, err := privValidator.GetPubKey()
// 	require.NoError(t, err)

// 	// create validator set with single validator
// 	validator := tmtypes.NewValidator(pubKey, 1)
// 	valSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{validator})

// 	// generate genesis account
// 	senderPrivKey := secp256k1.GenPrivKey()
// 	acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0)
// 	balance := banktypes.Balance{
// 		Address: acc.GetAddress().String(),
// 		Coins:   sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(100000000000000))),
// 	}

// 	genesisState := NewDefaultGenesisState(gapp.AppCodec())
// 	genesisState = genesisStateWithValSet(t, gapp, genesisState, valSet, []authtypes.GenesisAccount{acc}, balance)
// 	// init chain must be called to stop deliverState from being nil
// 	stateBytes, err := tmjson.MarshalIndent(genesisState, "", " ")
// 	require.NoError(t, err)

// 	_, err = gapp.InitChain(
// 		&abci.RequestInitChain{
// 			Validators:      []abci.ValidatorUpdate{},
// 			ConsensusParams: simapp.DefaultConsensusParams,
// 			AppStateBytes:   stateBytes,
// 		},
// 	)
// 	require.NoError(t, err, "InitChain must not fail")

// 	_, err = gapp.Commit()
// 	require.NoError(t, err, "Commit must not fail")

// 	_, err = gapp.ExportAppStateAndValidators(false, []string{}, []string{})
// 	require.NoError(t, err, "ExportAppStateAndValidators should not have an error")
// }

// ensure that blocked addresses are properly set in bank keeper
func TestBlockedAddrs(t *testing.T) {
	db := tmdb.NewMemDB()
	gapp := NewArchwayApp(log.NewTestLogger(t), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, MakeEncodingConfig(), EmptyBaseAppOptions{}, emptyWasmOpts)

	for acc := range BlockedAddresses() {
		var addr sdk.AccAddress
		if modAddr, err := sdk.AccAddressFromBech32(acc); err == nil {
			addr = modAddr
		} else {
			addr = gapp.Keepers.AccountKeeper.GetModuleAddress(acc)
		}

		require.True(
			t,
			gapp.Keepers.BankKeeper.BlockedAddr(addr),
			fmt.Sprintf("ensure that blocked addresses are properly set in bank keeper: %s should be blocked", acc),
		)
	}
}

func TestGetMaccPerms(t *testing.T) {
	dup := GetMaccPerms()
	require.Equal(t, maccPerms, dup, "duplicated module account permissions differed from actual module account permissions")
}

// func genesisStateWithValSet(t *testing.T,
// 	app *ArchwayApp, genesisState GenesisState,
// 	valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount,
// 	balances ...banktypes.Balance,
// ) GenesisState {
// 	t.Helper()
// 	// set genesis accounts
// 	authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
// 	genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenesis)

// 	validators := make([]stakingtypes.Validator, 0, len(valSet.Validators))
// 	delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators))

// 	bondAmt := sdk.DefaultPowerReduction

// 	for _, val := range valSet.Validators {
// 		pk, err := cryptocodec.FromCmtPubKeyInterface(val.PubKey)
// 		require.NoError(t, err)
// 		pkAny, err := codectypes.NewAnyWithValue(pk)
// 		require.NoError(t, err)
// 		validator := stakingtypes.Validator{
// 			OperatorAddress:   sdk.ValAddress(val.Address).String(),
// 			ConsensusPubkey:   pkAny,
// 			Jailed:            false,
// 			Status:            stakingtypes.Bonded,
// 			Tokens:            bondAmt,
// 			DelegatorShares:   math.LegacyOneDec(),
// 			Description:       stakingtypes.Description{},
// 			UnbondingHeight:   int64(0),
// 			UnbondingTime:     time.Unix(0, 0).UTC(),
// 			Commission:        stakingtypes.NewCommission(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec()),
// 			MinSelfDelegation: math.ZeroInt(),
// 		}
// 		validators = append(validators, validator)
// 		delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress().String(), sdk.ValAddress(val.Address).String(), math.LegacyOneDec()))
// 	}
// 	// set validators and delegations
// 	stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations)
// 	genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(stakingGenesis)

// 	totalSupply := sdk.NewCoins()
// 	for _, b := range balances {
// 		// add genesis acc tokens to total supply
// 		totalSupply = totalSupply.Add(b.Coins...)
// 	}

// 	for range delegations {
// 		// add delegated tokens to total supply
// 		totalSupply = totalSupply.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt))
// 	}

// 	// add bonded amount to bonded pool module account
// 	balances = append(balances, banktypes.Balance{
// 		Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(),
// 		Coins:   sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)},
// 	})

// 	// update total supply
// 	bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}, banktypes.DefaultGenesisState().SendEnabled)
// 	genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis)

// 	return genesisState
// }
