package commands

import (
	"fmt"

	"github.com/muyue/muyue/internal/config"
	"github.com/muyue/muyue/internal/mcp"
	"github.com/spf13/cobra"
)

var mcpCmd = &cobra.Command{
	Use:   "mcp",
	Short: "MCP management",
}

func init() {
	rootCmd.AddCommand(mcpCmd)
	mcpCmd.AddCommand(&cobra.Command{
		Use:   "config",
		Short: "Generate MCP configs for Crush + Claude Code",
		RunE:  runMCPConfig,
	})
	mcpCmd.AddCommand(&cobra.Command{
		Use:   "scan",
		Short: "Scan available MCP servers",
		RunE:  runMCPScan,
	})
}

func runMCPConfig(cmd *cobra.Command, args []string) error {
	cfg, err := config.Load()
	if err != nil {
		return err
	}
	if err := mcp.ConfigureAll(cfg); err != nil {
		return err
	}
	fmt.Println("MCP configs generated for Crush and Claude Code")
	return nil
}

func runMCPScan(cmd *cobra.Command, args []string) error {
	servers := mcp.ScanServers()
	fmt.Printf("%-25s %-15s %-10s\n", "Name", "Category", "Status")
	fmt.Println("──────────────────────────────────────────")
	for _, s := range servers {
		status := "✗ missing"
		if s.Installed {
			status = "✓ installed"
		}
		fmt.Printf("%-25s %-15s %-10s\n", s.Name, s.Category, status)
	}
	return nil
}