package cli

import (
    <%= for (goImport) in mergeGoImports(fields) { %>
    <%= goImport.Alias %> "<%= goImport.Name %>"<% } %>
	"time"

	"github.com/cosmos/cosmos-sdk/client"
	"github.com/cosmos/cosmos-sdk/client/flags"
	"github.com/cosmos/cosmos-sdk/client/tx"
	"github.com/spf13/cobra"

	"<%= ModulePath %>/x/<%= moduleName %>/types"
)

// CmdSend<%= packetName.PascalCase %>() returns the <%= packetName.PascalCase %> send packet command.
// This command does not use AutoCLI because it gives a better UX to do not.
func CmdSend<%= packetName.PascalCase %>() *cobra.Command {
	flagPacketTimeoutTimestamp := "packet-timeout-timestamp"

	cmd := &cobra.Command{
		Use:   "send-<%= packetName.Kebab %> [src-port] [src-channel] <%= fields.CLIUsage() %>",
		Short: "Send a <%= packetName.Original %> over IBC",
		Args:  cobra.ExactArgs(<%= len(fields) + 2 %>),
		RunE: func(cmd *cobra.Command, args []string) error {
			clientCtx, err := client.GetClientTxContext(cmd)
			if err != nil {
				return err
			}

			<%= MsgSigner.LowerCamel %> := clientCtx.GetFromAddress().String()
            srcPort := args[0]
            srcChannel := args[1]

            <%= for (i, field) in fields { %> <%= field.CLIArgs("arg", i+2) %>
      		<% } %>

            // Get the relative timeout timestamp
            timeoutTimestamp, err := cmd.Flags().GetUint64(flagPacketTimeoutTimestamp)
            if err != nil {
                return err
            }

			if timeoutTimestamp != 0 {
				now := time.Now().UnixNano()
				if now <= 0 {
					return err
				}
				timeoutTimestamp = uint64(now) + timeoutTimestamp
			}

			msg := types.NewMsgSend<%= packetName.PascalCase %>(<%= MsgSigner.LowerCamel %>, srcPort, srcChannel, timeoutTimestamp<%= for (i, field) in fields { %>, arg<%= field.Name.PascalCase %><% } %>)
			
			return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
		},
	}

	cmd.Flags().Uint64(flagPacketTimeoutTimestamp, DefaultRelativePacketTimeoutTimestamp, "Packet timeout timestamp in nanoseconds. Default is 10 minutes.")
	flags.AddTxFlagsToCmd(cmd)

    return cmd
}
