package cmd

import (
	"fmt"
	"os"
	"strings"
	"text/tabwriter"

	"github.com/spf13/cobra"
)

var toolsCmd = &cobra.Command{
	Use:   "tools",
	Short: "List tools available on the remote AMCS server",
	RunE: func(cmd *cobra.Command, _ []string) error {
		session, err := connectRemote(cmd.Context())
		if err != nil {
			return err
		}
		defer func() { _ = session.Close() }()

		res, err := session.ListTools(cmd.Context(), nil)
		if err != nil {
			return fmt.Errorf("list tools: %w", err)
		}

		w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
		fmt.Fprintln(w, "NAME\tDESCRIPTION")
		for _, tool := range res.Tools {
			fmt.Fprintf(w, "%s\t%s\n", tool.Name, strings.TrimSpace(tool.Description))
		}
		return w.Flush()
	},
}

func init() {
	rootCmd.AddCommand(toolsCmd)
}
