import { NextResponse } from "next/server";
import { auth } from "@/lib/auth";

const PROXY_URL = process.env.LLMHOSTS_PROXY_URL || "http://localhost:4000";

export async function POST() {
  const session = await auth();

  if (!session?.user?.id) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  try {
    const resp = await fetch(`${PROXY_URL}/api/topology/ping`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
    });

    if (!resp.ok) {
      return NextResponse.json(
        { error: "Failed to trigger ping on proxy" },
        { status: resp.status },
      );
    }

    const data = await resp.json();
    return NextResponse.json(data);
  } catch {
    return NextResponse.json(
      { error: "Proxy unreachable" },
      { status: 503 },
    );
  }
}
