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

// Standard Next.js-safe Prisma singleton: without this, hot-reload in dev
// (and some serverless cold-start patterns) can open a new connection pool
// per request and exhaust the database's connection limit.
const globalForPrisma = globalThis as unknown as { prisma?: PrismaClient };

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

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