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

// Customer password fields are omitted by default everywhere — a query that actually needs to verify
// a password (customer-password-auth.ts) opts back in per-call with `omit: { passwordHash: false, passwordSalt: false }`.
// This is a safety net against ever accidentally serializing a hash/salt into a page passed to a Client Component.
function createPrismaClient() {
  return new PrismaClient({
    omit: { customer: { passwordHash: true, passwordSalt: true } },
  });
}

// The global cache must be typed from the client above (omit config and all) rather than plain
// PrismaClient — otherwise `??` between the two produces a type TypeScript can't reconcile and
// `next build`'s TypeScript pass fails outright (dev mode and a bare `tsc --noEmit` don't catch this).
type AppPrismaClient = ReturnType<typeof createPrismaClient>;
const globalForPrisma = globalThis as unknown as { prisma?: AppPrismaClient };

export const prisma = globalForPrisma.prisma ?? createPrismaClient();

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