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

export const maxDuration = 120;

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

    const result = await trackAiTask("仿寫爆款貼文", () => replicateScanItem(scanItemId));
    return NextResponse.json(result);
  } catch (error) {
    const message = error instanceof Error ? error.message : "複製爆款失敗";
    return NextResponse.json({ error: message }, { status: 500 });
  }
}
