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

/**
 * GET /api/lan-nodes
 *
 * Proxies to llmhosts backend LAN discovery endpoint.
 * Returns mDNS-discovered LLMHosts instances on the local network.
 */
export async function GET() {
  try {
    const backendUrl =
      process.env.LLMHOSTS_BACKEND_URL || "http://llmhosts:4000";

    const res = await fetch(`${backendUrl}/api/lan-nodes`, {
      headers: {
        Accept: "application/json",
      },
    });

    if (!res.ok) {
      logger.error({ action: 'lan_nodes_fetch', status: res.status, statusText: res.statusText }, 'Failed to fetch LAN nodes from backend');
      return NextResponse.json({ nodes: [], count: 0 }, { status: 200 });
    }

    const data = await res.json();
    return NextResponse.json(data);
  } catch (error) {
    logger.error({ err: error, action: 'lan_nodes_fetch' }, 'Error fetching LAN nodes from backend');
    return NextResponse.json({ nodes: [], count: 0 }, { status: 200 });
  }
}
