package v3_test

import (
	"encoding/json"
	"testing"

	"github.com/stretchr/testify/require"

	sdkmath "cosmossdk.io/math"

	"git.cw.tr/mukan-network/mukan-sdk/client"
	moduletestutil "git.cw.tr/mukan-network/mukan-sdk/types/module/testutil"
	"git.cw.tr/mukan-network/mukan-sdk/x/distribution"
	v3 "git.cw.tr/mukan-network/mukan-sdk/x/distribution/migrations/v3"
	"git.cw.tr/mukan-network/mukan-sdk/x/distribution/types"
)

func TestMigrateJSON(t *testing.T) {
	encodingConfig := moduletestutil.MakeTestEncodingConfig(distribution.AppModuleBasic{})
	clientCtx := client.Context{}.
		WithInterfaceRegistry(encodingConfig.InterfaceRegistry).
		WithTxConfig(encodingConfig.TxConfig).
		WithCodec(encodingConfig.Codec)

	distrGenState := types.DefaultGenesisState()

	oldDistrState := distrGenState
	oldDistrState.Params.BaseProposerReward = sdkmath.LegacyNewDecWithPrec(1, 2)
	oldDistrState.Params.BonusProposerReward = sdkmath.LegacyNewDecWithPrec(4, 2)

	migrated := v3.MigrateJSON(oldDistrState)
	require.Equal(t, migrated, distrGenState)

	bz, err := clientCtx.Codec.MarshalJSON(migrated)
	require.NoError(t, err)

	// Indent the JSON bz correctly.
	var jsonObj map[string]any
	err = json.Unmarshal(bz, &jsonObj)
	require.NoError(t, err)
	indentedBz, err := json.MarshalIndent(jsonObj, "", "\t")
	require.NoError(t, err)

	expected := `{
	"delegator_starting_infos": [],
	"delegator_withdraw_infos": [],
	"fee_pool": {
		"community_pool": []
	},
	"outstanding_rewards": [],
	"params": {
		"base_proposer_reward": "0.000000000000000000",
		"bonus_proposer_reward": "0.000000000000000000",
		"community_tax": "0.020000000000000000",
		"withdraw_addr_enabled": true
	},
	"previous_proposer": "",
	"validator_accumulated_commissions": [],
	"validator_current_rewards": [],
	"validator_historical_rewards": [],
	"validator_slash_events": []
}`

	require.Equal(t, expected, string(indentedBz))
}
