package cli

import (
	"encoding/base64"

	"github.com/spf13/cobra"
	"github.com/spf13/pflag"

	"github.com/cosmos/cosmos-sdk/client"
	sdk "github.com/cosmos/cosmos-sdk/types"

	"github.com/bitsongofficial/go-bitsong/btsgutils/btsgcli"
	"github.com/bitsongofficial/go-bitsong/x/smart-account/types"
)

func NewTxCmd() *cobra.Command {
	txCmd := btsgcli.TxIndexCmd(types.ModuleName)
	btsgcli.AddTxCmd(txCmd, NewAddAuthentiactorCmd)
	btsgcli.AddTxCmd(txCmd, NewRemoveAuthentiactorCmd)
	return txCmd
}

func NewAddAuthentiactorCmd() (*btsgcli.TxCliDesc, *types.MsgAddAuthenticator) {
	return &btsgcli.TxCliDesc{
		Use:   "add-authenticator",
		Short: "add an authenticator for an address",
		Long:  "",
		Example: `
			osmosisd tx authenticator add-authenticator SigVerification <pubkey> --from val \
			--chain-id osmosis-1 -b sync --keyring-backend test \
			--fees 1000uosmo
		`,
		ParseAndBuildMsg: BuildAddAuthenticatorMsg,
	}, &types.MsgAddAuthenticator{}
}

func NewRemoveAuthentiactorCmd() (*btsgcli.TxCliDesc, *types.MsgRemoveAuthenticator) {
	return &btsgcli.TxCliDesc{
		Use:   "remove-authenticator",
		Short: "add an authenticator for an address",
		Long:  "",
		Example: `
			osmosisd tx authenticator remove-authenticator 1 --from val \
			--chain-id osmosis-1 -b sync --keyring-backend test \
			--fees 1000uosmo
		`,
	}, &types.MsgRemoveAuthenticator{}
}

func BuildAddAuthenticatorMsg(
	clientCtx client.Context,
	args []string,
	flags *pflag.FlagSet,
) (sdk.Msg, error) {
	authenticatorType := args[0]
	pubKeyEncoded := args[1]

	pubKeyBytes, err := base64.StdEncoding.DecodeString(pubKeyEncoded)
	if err != nil {
		return nil, err
	}

	return &types.MsgAddAuthenticator{
		AuthenticatorType: authenticatorType,
		Data:              pubKeyBytes,
		Sender:            clientCtx.GetFromAddress().String(),
	}, nil
}
