import { prisma } from "@codeflash-ai/common"
import * as Sentry from "@sentry/nextjs"

const isProduction = process.env.NODE_ENV === "production"
const SLOW_QUERY_THRESHOLD_MS = 500

// Log slow queries in development
if (!isProduction) {
  ;(prisma as any).$on("query", (e: any) => {
    if (e.duration > SLOW_QUERY_THRESHOLD_MS) {
      console.warn(`[Prisma] Slow query (${e.duration}ms): ${e.query}`)
    }
  })
}

// Forward Prisma warnings and errors to Sentry
;(prisma as any).$on("warn", (e: any) => {
  console.warn("[Prisma] Warning:", e.message)
})
;(prisma as any).$on("error", (e: any) => {
  console.error("[Prisma] Error:", e.message)
  Sentry.captureException(new Error(`Prisma error: ${e.message}`))
})

export { prisma }
