// Test Prisma connection directly
import { PrismaClient } from './lib/generated/prisma/index.js';

const prisma = new PrismaClient({
  log: ['query', 'error'],
  datasources: {
    db: {
      url: 'postgresql://mawidi:mawidi_db_pass_2025@localhost:5432/mawidi_production'
    }
  }
});

async function test() {
  console.log('Testing Prisma connection...\n');
  
  // Test connection
  await prisma.$connect();
  console.log('✅ Connected to database\n');
  
  // Get database info
  const dbInfo = await prisma.$queryRaw`SELECT current_database() as db, current_user`;
  console.log('Database:', dbInfo);
  
  // Count all users
  const count = await prisma.user.count();
  console.log('\nTotal users via Prisma.user.count():', count);
  
  // Raw count
  const rawCount = await prisma.$queryRaw`SELECT COUNT(*)::int as count FROM users`;
  console.log('Total users via raw SQL:', rawCount[0].count);
  
  // List all users
  const allUsers = await prisma.user.findMany({
    select: { id: true, email: true, name: true },
    orderBy: { createdAt: 'desc' },
    take: 10
  });
  
  console.log('\nLatest 10 users via Prisma:');
  allUsers.forEach(u => console.log(`  - ${u.email} (${u.id})`));
  
  // Try to find the specific user
  const sibuf = await prisma.user.findUnique({
    where: { email: 'sibuf@mailinator.com' }
  });
  console.log('\nCan find sibuf@mailinator.com?', !!sibuf);
  if (sibuf) {
    console.log('  User:', sibuf);
  }
  
  await prisma.$disconnect();
}

test().catch(e => {
  console.error('Error:', e.message);
  process.exit(1);
});
