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

export const dynamic = "force-dynamic";

export async function GET(
  _req: Request,
  {
    params,
  }: { params: Promise<{ id: string; initId: string; taskFilename: string }> }
) {
  const { id, initId, taskFilename } = 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 decoded = decodeURIComponent(taskFilename);
  const data = await loadTaskMarkdown(cfg.autobuilderDir, initId, decoded);
  if (!data) {
    return NextResponse.json({ error: "Task not found" }, { status: 404 });
  }

  return NextResponse.json({
    filename: decoded,
    content: data.content,
    createdAt: data.createdAt,
    modifiedAt: data.modifiedAt,
  });
}
