package cmd

import (
	"context"
	"fmt"
	"strings"

	"github.com/modelcontextprotocol/go-sdk/mcp"
	"github.com/spf13/cobra"
)

var sseCmd = &cobra.Command{
	Use:   "sse",
	Short: "Run a stdio MCP bridge backed by a remote AMCS server using SSE transport (widely supported by hosted MCP clients)",
	RunE: func(cmd *cobra.Command, _ []string) error {
		ctx := cmd.Context()

		if err := requireServer(); err != nil {
			return err
		}

		client := mcp.NewClient(&mcp.Implementation{Name: "amcs-cli", Version: "0.0.1"}, nil)
		transport := &mcp.SSEClientTransport{
			Endpoint:   sseEndpointURL(),
			HTTPClient: newHTTPClient(),
		}

		verboseLogf("connecting to SSE endpoint %s", sseEndpointURL())
		remote, err := client.Connect(ctx, transport, nil)
		if err != nil {
			return fmt.Errorf("connect to AMCS SSE endpoint: %w", err)
		}
		defer func() { _ = remote.Close() }()
		verboseLogf("connected to SSE endpoint %s", sseEndpointURL())

		tools, err := remote.ListTools(ctx, nil)
		if err != nil {
			return fmt.Errorf("load remote tools: %w", err)
		}

		server := mcp.NewServer(&mcp.Implementation{
			Name:    "amcs-cli",
			Title:   "AMCS CLI Bridge (SSE)",
			Version: "0.0.1",
		}, nil)

		for _, tool := range tools.Tools {
			remoteTool := tool
			server.AddTool(&mcp.Tool{
				Name:         remoteTool.Name,
				Description:  remoteTool.Description,
				InputSchema:  remoteTool.InputSchema,
				OutputSchema: remoteTool.OutputSchema,
				Annotations:  remoteTool.Annotations,
			}, func(ctx context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) {
				return remote.CallTool(ctx, &mcp.CallToolParams{
					Name:      req.Params.Name,
					Arguments: req.Params.Arguments,
				})
			})
		}

		session, err := server.Connect(ctx, &mcp.StdioTransport{}, nil)
		if err != nil {
			return fmt.Errorf("start stdio bridge: %w", err)
		}
		defer func() { _ = session.Close() }()
		verboseLogf("sse stdio bridge ready")
		verboseLogf("waiting for MCP commands on stdin")

		<-ctx.Done()
		return nil
	},
}

func sseEndpointURL() string {
	base := strings.TrimRight(strings.TrimSpace(cfg.Server), "/")
	if strings.HasSuffix(base, "/mcp") {
		base = strings.TrimSuffix(base, "/mcp")
	}
	if strings.HasSuffix(base, "/sse") {
		return base
	}
	return base + "/sse"
}

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