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

export const maxDuration = 120;

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

    const assessments = await trackAiTask("整理海巡結果", () => applyQualityFilter(scanId));
    return NextResponse.json({ assessments });
  } catch (error) {
    const message = error instanceof Error ? error.message : "整理海巡結果失敗";
    return NextResponse.json({ error: message }, { status: 500 });
  }
}
