import { PrismaClient } from '@prisma/client';
import { PrismaClient as PrismaClientAuth } from '../node_modules/.prisma/client-auth';

const globalForPrisma = global as unknown as { 
  prisma: PrismaClient;
  prismaWrite: PrismaClient;
  prismaAuth: PrismaClientAuth;
};

// Read-only client (uses DATABASE_URL) - connects to punimtag database
export const prisma =
  globalForPrisma.prisma ||
  new PrismaClient({
    log: process.env.NODE_ENV === 'development' ? ['error', 'warn'] : ['error'],
  });

// Write-capable client (uses DATABASE_URL_WRITE if available, otherwise DATABASE_URL)
// This is kept for backward compatibility but should not be used for auth operations
export const prismaWrite =
  globalForPrisma.prismaWrite ||
  new PrismaClient({
    datasourceUrl: process.env.DATABASE_URL_WRITE || process.env.DATABASE_URL,
    log: process.env.NODE_ENV === 'development' ? ['error', 'warn'] : ['error'],
  });

// Auth client - connects to separate punimtag_auth database (uses DATABASE_URL_AUTH)
// This database contains users and pending_identifications tables
export const prismaAuth =
  globalForPrisma.prismaAuth ||
  new PrismaClientAuth({
    datasourceUrl: process.env.DATABASE_URL_AUTH,
    log: process.env.NODE_ENV === 'development' ? ['error', 'warn'] : ['error'],
  });

if (process.env.NODE_ENV !== 'production') {
  globalForPrisma.prisma = prisma;
  globalForPrisma.prismaWrite = prismaWrite;
  globalForPrisma.prismaAuth = prismaAuth;
}





