import { NextResponse } from "next/server";
import { requireSuperadmin } from "@/lib/admin-auth";
import { prisma } from "@/lib/prisma";
import { MIN_SAMPLE } from "@/lib/services/benchmark-engine";

export const dynamic = "force-dynamic";

export async function GET() {
  const denied = await requireSuperadmin();
  if (denied) return denied;

  const grouped = await prisma.brand.groupBy({
    by: ["industry"],
    _count: { _all: true },
    where: { industry: { not: null } },
    orderBy: { _count: { industry: "desc" } },
  });

  const industries = grouped
    .filter((g) => g.industry !== null)
    .map((g) => ({
      value: g.industry as string,
      label: g.industry as string,
      count: g._count._all,
      meetsThreshold: g._count._all >= MIN_SAMPLE,
    }));

  return NextResponse.json({ industries, minSample: MIN_SAMPLE });
}
