package cmd

import (
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"strings"

	"github.com/modelcontextprotocol/go-sdk/mcp"
	"github.com/spf13/cobra"
	"gopkg.in/yaml.v3"
)

var argFlags []string

var callCmd = &cobra.Command{
	Use:   "call <tool>",
	Short: "Call a remote AMCS tool",
	Args:  cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		toolName := args[0]
		toolArgs, err := parseArgs(argFlags)
		if err != nil {
			return err
		}

		session, err := connectRemote(cmd.Context())
		if err != nil {
			return err
		}
		defer func() { _ = session.Close() }()

		res, err := session.CallTool(cmd.Context(), &mcp.CallToolParams{Name: toolName, Arguments: toolArgs})
		if err != nil {
			return fmt.Errorf("call tool %q: %w", toolName, err)
		}
		return printOutput(res)
	},
}

func init() {
	callCmd.Flags().StringArrayVar(&argFlags, "arg", nil, "Tool argument in key=value format (repeatable)")
	rootCmd.AddCommand(callCmd)
}

func parseArgs(items []string) (map[string]any, error) {
	result := make(map[string]any, len(items))
	for _, item := range items {
		key, value, ok := strings.Cut(item, "=")
		if !ok || strings.TrimSpace(key) == "" {
			return nil, fmt.Errorf("invalid --arg %q: want key=value", item)
		}
		result[key] = parseScalar(value)
	}
	return result, nil
}

func parseScalar(s string) any {
	if s == "true" || s == "false" {
		b, _ := strconv.ParseBool(s)
		return b
	}
	if i, err := strconv.ParseInt(s, 10, 64); err == nil {
		return i
	}
	if f, err := strconv.ParseFloat(s, 64); err == nil && strings.ContainsAny(s, ".eE") {
		return f
	}
	var v any
	if err := json.Unmarshal([]byte(s), &v); err == nil {
		switch v.(type) {
		case map[string]any, []any, float64, bool, nil:
			return v
		}
	}
	return s
}

func printOutput(v any) error {
	switch outputFlag {
	case "yaml":
		data, err := yaml.Marshal(v)
		if err != nil {
			return fmt.Errorf("marshal yaml: %w", err)
		}
		_, err = os.Stdout.Write(data)
		return err
	default:
		data, err := json.MarshalIndent(v, "", "  ")
		if err != nil {
			return fmt.Errorf("marshal json: %w", err)
		}
		data = append(data, '\n')
		_, err = os.Stdout.Write(data)
		return err
	}
}
