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

// Load environment variables
dotenv.config({ path: '.env' });

const prisma = new PrismaClientAuth({
  datasourceUrl: process.env.DATABASE_URL_AUTH,
});

async function testAdminCheck() {
  try {
    console.log('Testing admin user check...\n');

    // Find admin user
    const admin = await prisma.user.findUnique({
      where: { email: 'admin@admin.com' },
      select: {
        id: true,
        email: true,
        name: true,
        isAdmin: true,
        hasWriteAccess: true,
      },
    });

    if (!admin) {
      console.log('❌ Admin user not found!');
      return;
    }

    console.log('✅ Admin user found:');
    console.log('  ID:', admin.id);
    console.log('  Email:', admin.email);
    console.log('  Is Admin:', admin.isAdmin);
    console.log('  Has Write Access:', admin.hasWriteAccess);

    // Test querying all users (like the API does)
    console.log('\nTesting user list query...');
    const users = await prisma.user.findMany({
      select: {
        id: true,
        email: true,
        name: true,
        isAdmin: true,
        hasWriteAccess: true,
        createdAt: true,
        updatedAt: true,
      },
      orderBy: {
        createdAt: 'desc',
      },
    });

    console.log(`✅ Successfully queried ${users.length} users`);
    users.forEach((user, index) => {
      console.log(`  ${index + 1}. ${user.email} (Admin: ${user.isAdmin}, Write: ${user.hasWriteAccess})`);
    });

    console.log('\n✅ All checks passed!');
  } catch (error: any) {
    console.error('\n❌ Error:', error.message);
    console.error('Stack:', error.stack);
  } finally {
    await prisma.$disconnect();
  }
}

testAdminCheck();

