import { PrismaClient } from "@prisma/client";

// Singleton-Pattern: Im Dev-Modus erzeugt Next.js bei jedem Hot-Reload
// neue Modul-Instanzen → ohne Cache landen schnell zu viele DB-Connections
// im Pool ("Too many connections"). Wir hängen den Client an `globalThis`.
const globalForPrisma = globalThis as unknown as {
  prisma: PrismaClient | undefined;
};

export const prisma =
  globalForPrisma.prisma ??
  new PrismaClient({
    log:
      process.env.NODE_ENV === "development"
        ? ["error", "warn"]
        : ["error"],
  });

if (process.env.NODE_ENV !== "production") {
  globalForPrisma.prisma = prisma;
}
