import { NextResponse } from "next/server";
import { getProjectConfigs, loadInitiatives } from "@/lib/autobuilder";

export const dynamic = "force-dynamic";

export async function GET(
  _req: Request,
  { params }: { params: Promise<{ id: string; initId: string }> }
) {
  const { id, initId } = await params;
  const configs = await getProjectConfigs();
  const cfg = configs.find((c) => c.name === id);
  if (!cfg) return NextResponse.json({ error: "Project not found" }, { status: 404 });

  const initiatives = await loadInitiatives(cfg.autobuilderDir);
  const initiative = initiatives.find(
    (i) => i.id === initId || i.filename.replace(".md", "") === initId
  );
  if (!initiative) return NextResponse.json({ error: "Initiative not found" }, { status: 404 });
  return NextResponse.json(initiative);
}
