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

const prisma = new PrismaClient();

async function main() {
  console.log('Testing database connection...');

  // Test 1: Count tools
  console.time('Count tools');
  const count = await prisma.tool.count();
  console.timeEnd('Count tools');
  console.log(`Total tools: ${count}`);

  // Test 2: Fetch first 20 tools (same as API)
  console.time('Fetch 20 tools');
  const tools = await prisma.tool.findMany({
    orderBy: [{ qualityScore: 'desc' }, { npmDownloadsLastMonth: 'desc' }, { createdAt: 'desc' }],
    take: 20,
  });
  console.timeEnd('Fetch 20 tools');
  console.log(`Fetched ${tools.length} tools`);

  if (tools.length > 0) {
    console.log('\nFirst tool:', tools[0].npmPackageName);
    console.log('Quality score:', tools[0].qualityScore);
  } else {
    console.log('\n⚠️  No tools found in database!');
  }
}

main()
  .catch((e) => {
    console.error('❌ Database error:', e.message);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
