import { NextResponse } from "next/server";
import { listHosts, addHost } from "@/lib/apps-registry";

export async function GET() {
  try {
    const hosts = await listHosts();
    return NextResponse.json({ hosts });
  } 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, address, description } = body;
    if (!name || !address) {
      return NextResponse.json({ error: "name and address are required" }, { status: 400 });
    }
    const host = await addHost({ name, address, description });
    return NextResponse.json({ host }, { status: 201 });
  } catch (err) {
    return NextResponse.json({ error: err instanceof Error ? err.message : "Unknown error" }, { status: 500 });
  }
}
