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

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

export async function PUT(request: Request, { params }: { params: Promise<{ typeId: string }> }) {
  try {
    const { typeId } = await params;
    const { fields } = await request.json();
    if (!Array.isArray(fields)) {
      return NextResponse.json({ error: "fields must be an array" }, { status: 400 });
    }
    const definition = await updateTypeDefinition(typeId, fields);
    return NextResponse.json({ fields: definition.configFields });
  } catch (err) {
    return NextResponse.json({ error: err instanceof Error ? err.message : "Unknown error" }, { status: 500 });
  }
}
