import { createToolsClient, type ToolsServerPaths } from "@argent/tools-client";
import { formatSchemaUsage, type JsonSchema } from "./flag-parser.js";

export interface ToolsCommandOptions {
  paths: ToolsServerPaths;
}

function summarize(description: string | undefined, max = 80): string {
  if (!description) return "";
  const firstLine = description.split("\n", 1)[0]!.trim();
  if (firstLine.length <= max) return firstLine;
  return firstLine.slice(0, max - 1).trimEnd() + "…";
}

export async function tools(argv: string[], options: ToolsCommandOptions): Promise<void> {
  const { fetchTool, fetchTools } = createToolsClient({ paths: options.paths });

  async function listTools(json: boolean): Promise<void> {
    const list = await fetchTools();
    if (json) {
      console.log(JSON.stringify(list, null, 2));
      return;
    }

    if (list.length === 0) {
      console.log("(no tools registered)");
      return;
    }

    const sorted = [...list].sort((a, b) => a.name.localeCompare(b.name));
    const maxName = sorted.reduce((m, t) => Math.max(m, t.name.length), 0);
    for (const t of sorted) {
      const summary = summarize(t.description);
      console.log(`  ${t.name.padEnd(maxName, " ")}  ${summary}`);
    }
    console.log(`\n${sorted.length} tools. Run \`argent tools describe <name>\` for details.`);
  }

  async function describeTool(name: string, json: boolean): Promise<void> {
    const meta = await fetchTool(name);
    if (!meta) {
      console.error(`Tool "${name}" not found. Run \`argent tools\` to list available tools.`);
      process.exit(1);
    }
    if (json) {
      console.log(JSON.stringify(meta, null, 2));
      return;
    }
    console.log(`Tool: ${meta.name}\n`);
    if (meta.description) console.log(`${meta.description.trim()}\n`);
    console.log("Flags:");
    console.log(formatSchemaUsage(meta.inputSchema as JsonSchema));
    if (meta.outputHint) console.log(`\nOutput hint: ${meta.outputHint}`);
  }

  function printUsage(): void {
    console.log(`Usage:
  argent tools                       List available tools
  argent tools describe <name>       Show one tool's flags and description

Options:
  --json                             Print machine-readable JSON
  --help, -h                         Show this help

Listing tools contacts the argent tool-server, starting one if none is running.
`);
  }

  const json = argv.includes("--json");
  // A help flag in the subcommand slot is a request for this command's usage.
  // It has to be recognised before anything contacts the tool-server, and it
  // must not swallow a later one: `argent tools describe <name> --help` asks
  // for that tool's flags, which `describeTool` prints.
  const isHelpFlag = (a: string) => a === "--help" || a === "-h";
  const positional = argv.filter((a) => !a.startsWith("--") || isHelpFlag(a));
  const sub = positional[0];

  if (sub !== undefined && isHelpFlag(sub)) {
    printUsage();
    return;
  }

  if (!sub) {
    await listTools(json);
    return;
  }

  if (sub === "describe") {
    const name = positional[1];
    if (!name) {
      console.error("Usage: argent tools describe <tool-name>");
      process.exit(1);
    }
    await describeTool(name, json);
    return;
  }

  console.error(`Unknown subcommand: tools ${sub}`);
  process.exit(1);
}
