package simulation

import (
	"context"

	"git.cw.tr/mukan-network/mukan-sdk/testutil/simsx"
	sdk "git.cw.tr/mukan-network/mukan-sdk/types"
	"git.cw.tr/mukan-network/mukan-sdk/x/protocolpool/types"
)

func MsgFundCommunityPoolFactory() simsx.SimMsgFactoryFn[*types.MsgFundCommunityPool] {
	return func(_ context.Context, testData *simsx.ChainDataSource, reporter simsx.SimulationReporter) ([]simsx.SimAccount, *types.MsgFundCommunityPool) {
		funder := testData.AnyAccount(reporter, simsx.WithSpendableBalance())
		fundAmount := funder.LiquidBalance().RandSubsetCoins(reporter)
		msg := types.NewMsgFundCommunityPool(fundAmount, funder.AddressBech32)
		return []simsx.SimAccount{funder}, msg
	}
}

// MsgCommunityPoolSpendFactory creates a gov proposal to send tokens from the community pool to a random account
func MsgCommunityPoolSpendFactory() simsx.SimMsgFactoryFn[*types.MsgCommunityPoolSpend] {
	return func(_ context.Context, testData *simsx.ChainDataSource, reporter simsx.SimulationReporter) ([]simsx.SimAccount, *types.MsgCommunityPoolSpend) {
		return nil, &types.MsgCommunityPoolSpend{
			Authority: testData.ModuleAccountAddress(reporter, "gov"),
			Recipient: testData.AnyAccount(reporter).AddressBech32,
			Amount:    must(sdk.ParseCoinsNormalized("100stake,2testtoken")),
		}
	}
}

func must[T any](r T, err error) T {
	if err != nil {
		panic(err)
	}
	return r
}
