import { NextResponse } from "next/server";
import { getTool, updateTool, deleteTool } from "@/lib/tools-menu-registry";

export async function GET(_request: Request, { params }: { params: Promise<{ toolId: string }> }) {
  try {
    const { toolId } = await params;
    const tool = await getTool(toolId);
    if (!tool) {
      return NextResponse.json({ error: "Tool not found" }, { status: 404 });
    }
    return NextResponse.json({ tool });
  } catch (err) {
    return NextResponse.json({ error: err instanceof Error ? err.message : "Unknown error" }, { status: 500 });
  }
}

export async function PUT(request: Request, { params }: { params: Promise<{ toolId: string }> }) {
  try {
    const { toolId } = await params;
    const updates = await request.json();
    const tool = await updateTool(toolId, updates);
    return NextResponse.json({ tool });
  } catch (err) {
    return NextResponse.json({ error: err instanceof Error ? err.message : "Unknown error" }, { status: 500 });
  }
}

export async function DELETE(_request: Request, { params }: { params: Promise<{ toolId: string }> }) {
  try {
    const { toolId } = await params;
    await deleteTool(toolId);
    return NextResponse.json({ ok: true });
  } catch (err) {
    return NextResponse.json({ error: err instanceof Error ? err.message : "Unknown error" }, { status: 500 });
  }
}
