import { NextResponse } from "next/server";
import { listTools, addTool } from "@/lib/tools-menu-registry";

export async function GET(request: Request) {
  try {
    const url = new URL(request.url);
    const typeId = url.searchParams.get("typeId") || undefined;
    const tools = await listTools({ typeId });
    return NextResponse.json({ tools });
  } catch (err) {
    return NextResponse.json({ error: err instanceof Error ? err.message : "Unknown error" }, { status: 500 });
  }
}

export async function POST(request: Request) {
  try {
    const body = await request.json();
    const { name, typeId, address, description, icon, color, config, status } = body;
    if (!name || !typeId) {
      return NextResponse.json({ error: "name and typeId are required" }, { status: 400 });
    }
    const tool = await addTool({ name, typeId, address, description, icon, color, config, status });
    return NextResponse.json({ tool }, { status: 201 });
  } catch (err) {
    return NextResponse.json({ error: err instanceof Error ? err.message : "Unknown error" }, { status: 500 });
  }
}
