package main

import (
	"context"
	"encoding/json"

	"github.com/mark3labs/mcp-go/mcp"
)

type showArgs struct {
	ID string `json:"id"`
}

func showTool() mcp.Tool {
	return mcp.NewTool("fn_show",
		mcp.WithDescription("Return a markdown card for the given function or type ID. Includes frontmatter-style header, description, signature, code block, examples and notes. Use after fn_search to fetch details."),
		mcp.WithString("id",
			mcp.Required(),
			mcp.Description("Registry ID (e.g. filter_slice_go_core, Result_go_core)."),
		),
	)
}

func (d *deps) handleShow(ctx context.Context, _ mcp.CallToolRequest, args showArgs) (*mcp.CallToolResult, error) {
	if args.ID == "" {
		return mcp.NewToolResultError("id is required"), nil
	}
	if f, err := d.db.GetFunction(args.ID); err == nil {
		md := truncate(renderFunctionMarkdown(f), 50_000)
		out := map[string]any{"id": f.ID, "entity": "function", "markdown": md}
		b, _ := json.MarshalIndent(out, "", "  ")
		return mcp.NewToolResultText(string(b)), nil
	}
	if t, err := d.db.GetType(args.ID); err == nil {
		md := truncate(renderTypeMarkdown(t), 50_000)
		out := map[string]any{"id": t.ID, "entity": "type", "markdown": md}
		b, _ := json.MarshalIndent(out, "", "  ")
		return mcp.NewToolResultText(string(b)), nil
	}
	return mcp.NewToolResultError("id not found: " + args.ID), nil
}
