import { NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { checkAiVisibility } from "@/lib/services/ai-visibility-checker";
export const dynamic = "force-dynamic";
export const maxDuration = 30;
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 || !body?.query) return NextResponse.json({ error: "brandId and query required" }, { status: 400 });
  try {
    const result = await checkAiVisibility(body.brandId, body.query);
    return NextResponse.json(result);
  } catch (err) {
    return NextResponse.json({ error: err instanceof Error ? err.message : "Check failed" }, { status: 500 });
  }
}
