package cli_test

import (
	"context"
	"encoding/base64"
	"testing"

	"github.com/stretchr/testify/require"

	"git.cw.tr/mukan-network/mukan-sdk/client"
	"git.cw.tr/mukan-network/mukan-sdk/testutil"
	sdk "git.cw.tr/mukan-network/mukan-sdk/types"
	moduletestutil "git.cw.tr/mukan-network/mukan-sdk/types/module/testutil"
	"git.cw.tr/mukan-network/mukan-sdk/x/auth"
	"git.cw.tr/mukan-network/mukan-sdk/x/auth/client/cli"
)

func TestGetCommandEncode(t *testing.T) {
	encodingConfig := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{})
	txConfig := encodingConfig.TxConfig
	cdc := encodingConfig.Codec

	cmd := cli.GetEncodeCommand()
	_ = testutil.ApplyMockIODiscardOutErr(cmd)

	// Build a test transaction
	builder := txConfig.NewTxBuilder()
	builder.SetGasLimit(50000)
	builder.SetFeeAmount(sdk.Coins{sdk.NewInt64Coin("atom", 150)})
	builder.SetMemo("foomemo")
	jsonEncoded, err := txConfig.TxJSONEncoder()(builder.GetTx())
	require.NoError(t, err)

	txFile := testutil.WriteToNewTempFile(t, string(jsonEncoded))
	txFileName := txFile.Name()

	ctx := context.Background()
	clientCtx := client.Context{}.
		WithTxConfig(txConfig).
		WithCodec(cdc)
	ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)

	cmd.SetArgs([]string{txFileName})
	err = cmd.ExecuteContext(ctx)
	require.NoError(t, err)
}

func TestGetCommandDecode(t *testing.T) {
	encodingConfig := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{})
	txConfig := encodingConfig.TxConfig
	cdc := encodingConfig.Codec

	clientCtx := client.Context{}.
		WithTxConfig(txConfig).
		WithCodec(cdc)

	cmd := cli.GetDecodeCommand()
	_ = testutil.ApplyMockIODiscardOutErr(cmd)

	clientCtx = clientCtx.WithTxConfig(txConfig)

	// Build a test transaction
	builder := txConfig.NewTxBuilder()
	builder.SetGasLimit(50000)
	builder.SetFeeAmount(sdk.Coins{sdk.NewInt64Coin("atom", 150)})
	builder.SetMemo("foomemo")

	// Encode transaction
	txBytes, err := clientCtx.TxConfig.TxEncoder()(builder.GetTx())
	require.NoError(t, err)

	// Convert the transaction into base64 encoded string
	base64Encoded := base64.StdEncoding.EncodeToString(txBytes)

	ctx := context.Background()
	ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)

	// Execute the command
	cmd.SetArgs([]string{base64Encoded})
	require.NoError(t, cmd.ExecuteContext(ctx))
}
