import { NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { runVisibilityAudit } from "@/lib/services/ai-visibility-checker";
export const dynamic = "force-dynamic";
export const maxDuration = 120;
export async function POST(req: Request) {
  const user = await getCurrentUser();
  if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  const body = await req.json().catch(() => null);
  if (!body?.brandId) return NextResponse.json({ error: "brandId required" }, { status: 400 });
  try {
    const summary = await runVisibilityAudit(body.brandId);
    return NextResponse.json(summary);
  } catch (err) {
    return NextResponse.json({ error: err instanceof Error ? err.message : "Audit failed" }, { status: 500 });
  }
}
