/**
 * GET /api/industry-intel/benchmarks?industry=healthcare&location=Texas
 * Returns benchmarks only when sampleSize >= 5.
 */

import { NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { prisma } from "@/lib/prisma";

export const dynamic = "force-dynamic";

export async function GET(req: Request) {
  const user = await getCurrentUser();
  if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });

  const { searchParams } = new URL(req.url);
  const industry = searchParams.get("industry");
  if (!industry) return NextResponse.json({ error: "industry required" }, { status: 400 });
  const location = searchParams.get("location") || undefined;

  const where: Record<string, unknown> = { industry, sampleSize: { gte: 5 } };
  if (location) where.location = location;

  const benchmarks = await prisma.industryBenchmark.findMany({
    where,
    orderBy: { computedAt: "desc" },
  });

  return NextResponse.json({ benchmarks });
}
