import { PrismaClient } from '@prisma/client'
import { PrismaPg } from '@prisma/adapter-pg'
// Updated: 2026-04-12 08:34 (Refreshed for new models)
import pg from 'pg'

const connectionString = process.env.DATABASE_URL!

function makePrisma() {
  const pool = new pg.Pool({ connectionString })
  const adapter = new PrismaPg(pool)
  return new PrismaClient({ adapter })
}

const globalForPrisma = globalThis as unknown as { prisma_v2: PrismaClient }

export const prisma = globalForPrisma.prisma_v2 || makePrisma()

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma_v2 = prisma

export default prisma
