import {PrismaClient} from '@prisma/client'

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

export const db =
	globalForPrisma.db ||
	new PrismaClient({
		log: [
			{
				emit: 'event',
				level: 'query'
			},
			{
				emit: 'stdout',
				level: 'error'
			},
			{
				emit: 'stdout',
				level: 'info'
			},
			{
				emit: 'stdout',
				level: 'warn'
			}
		]
	})

export function dbQueryLog() {
	db.$on('query' as never, async (e: any) => {
		console.log(e.query)
	})
}

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

// {
// 	errorFormat: 'pretty',
// 		log: [
// 	{
// 		emit: 'event',
// 		level: 'query'
// 	}
// ],
// 	omit: {
// 	user: {
// 		password: true,
// 			emailVerified: true,
// 			extendedData: true,
// 			role: true,
// 			locale: true,
// 			status: true,
// 			updatedAt: true,
// 			createdAt: true
// 	},
// 	store: {}
// }
// }

//
// db.$on('query' as never, async (e: any) => {
// 	console.debug(e.query)
// })
