/**
 * /api/graph/seed — initial node set + internal edges for the force-directed graph view.
 *
 * GET /api/graph/seed?limit=80&min_weight=2&classes=person,organization
 */
import { NextRequest } from "next/server";
import { getGraphSeed } from "@/lib/retrieval/graph";

export const runtime = "nodejs";
export const dynamic = "force-dynamic";

function json(data: unknown, status = 200) {
  return new Response(JSON.stringify(data), {
    status,
    headers: { "content-type": "application/json" },
  });
}

export async function GET(req: NextRequest) {
  const u = new URL(req.url);
  const limit = Math.min(Number(u.searchParams.get("limit") ?? 80), 300);
  const minWeight = Number(u.searchParams.get("min_weight") ?? 2);
  const classesParam = u.searchParams.get("classes");
  const classes = classesParam ? classesParam.split(",").filter(Boolean) : undefined;

  try {
    const { nodes, links } = await getGraphSeed({ limit, minWeight, classes });
    return json({ nodes, links, total: nodes.length });
  } catch (e) {
    return json({ error: "graph_unavailable", message: (e as Error).message }, 503);
  }
}
