package v3

import (
	"cosmossdk.io/core/store"
	sdkmath "cosmossdk.io/math"

	"git.cw.tr/mukan-network/mukan-sdk/codec"
	sdk "git.cw.tr/mukan-network/mukan-sdk/types"
	"git.cw.tr/mukan-network/mukan-sdk/x/distribution/exported"
	"git.cw.tr/mukan-network/mukan-sdk/x/distribution/types"
)

const (
	ModuleName = "distribution"
)

var ParamsKey = []byte{0x09}

// MigrateStore migrates the x/distribution module state from the consensus version 2 to
// version 3. Specifically, it takes the parameters that are currently stored
// and managed by the x/params module and stores them directly into the x/distribution
// module state.
func MigrateStore(ctx sdk.Context, storeService store.KVStoreService, legacySubspace exported.Subspace, cdc codec.BinaryCodec) error {
	store := storeService.OpenKVStore(ctx)
	var currParams types.Params
	legacySubspace.GetParamSet(ctx, &currParams)

	// reset unused params
	currParams.BaseProposerReward = sdkmath.LegacyZeroDec()
	currParams.BonusProposerReward = sdkmath.LegacyZeroDec()

	if err := currParams.ValidateBasic(); err != nil {
		return err
	}

	bz, err := cdc.Marshal(&currParams)
	if err != nil {
		return err
	}

	return store.Set(ParamsKey, bz)
}
