import { NextResponse } from "next/server";
import { generateMatrixForScan } from "@/lib/services/matrix";
import { trackAiTask } from "@/lib/jobs/track";

export const maxDuration = 180;

export async function POST(request: Request) {
  try {
    const { scanId, count } = (await request.json()) as { scanId?: string; count?: number };
    if (!scanId) {
      return NextResponse.json({ error: "缺少 scanId" }, { status: 400 });
    }

    const drafts = await trackAiTask("生成內容矩陣", () => generateMatrixForScan(scanId, count));
    return NextResponse.json({ drafts });
  } catch (error) {
    const message = error instanceof Error ? error.message : "生成內容矩陣失敗";
    return NextResponse.json({ error: message }, { status: 500 });
  }
}
