import { redirect } from "next/navigation";
import { unstable_noStore as noStore } from "next/cache";
import { prisma } from "./prisma";

export async function hasAdminUser() {
  noStore();
  try {
    const admin = await prisma.userRole.findFirst({
      where: { role: { name: "admin" } },
      select: { userId: true }
    });
    return Boolean(admin);
  } catch {
    return false;
  }
}

export async function requireInitialSetup() {
  if (!(await hasAdminUser())) {
    redirect("/setup");
  }
}
