import { NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
export const dynamic = "force-dynamic";

/** POST /api/ai-visibility/queries/bulk-delete  Body: { ids: string[] } */
export async function POST(req: Request) {
  const user = await getCurrentUser();
  if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  const { ids } = (await req.json()) as { ids: string[] };
  if (!Array.isArray(ids) || ids.length === 0) return NextResponse.json({ error: "ids required" }, { status: 400 });

  // Get query texts to clean up check results
  const queries = await prisma.aiVisibilityQuery.findMany({
    where: { id: { in: ids } },
    select: { query: true, brandId: true },
  });
  await prisma.aiVisibilityQuery.deleteMany({ where: { id: { in: ids } } });
  // Clean up check results
  for (const q of queries) {
    await prisma.aiVisibilityCheck.deleteMany({ where: { brandId: q.brandId, query: q.query } });
  }
  return NextResponse.json({ ok: true, deleted: ids.length });
}
