// Singleton Prisma client. Both apps import from here so connection pooling
// is shared and dev hot-reload doesn't leak connections.

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

declare global {
  // eslint-disable-next-line no-var
  var __viharPrisma: PrismaClient | undefined;
}

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

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

export * from '@prisma/client';

// Convenience helper - cache key for route distance lookups.
// Walking is symmetric so we always sort the place IDs.
export function buildRouteCacheKey(placeIdA: string, placeIdB: string): string {
  const [a, b] = [placeIdA, placeIdB].sort();
  return `${a}__${b}`;
}
