import { NextRequest, NextResponse } from "next/server";
import { listMcpServers, addMcpServer } from "@/lib/skills-registry";
import type { McpServerDefinition } from "@/lib/types";

export async function GET() {
  const servers = await listMcpServers();
  return NextResponse.json({ servers });
}

export async function POST(request: NextRequest) {
  try {
    const body = (await request.json()) as McpServerDefinition;

    if (!body.name || !body.command) {
      return NextResponse.json(
        { error: "Missing required fields: name, command" },
        { status: 400 }
      );
    }

    const server = await addMcpServer({
      name: body.name,
      description: body.description || "",
      command: body.command,
      args: body.args || [],
      env: body.env || {},
      agents: [],
    });

    return NextResponse.json({ server }, { status: 201 });
  } catch (err) {
    const message = err instanceof Error ? err.message : "Unknown error";
    return NextResponse.json({ error: message }, { status: 400 });
  }
}
