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

dotenv.config();

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

async function manuallyVerifyUser() {
  try {
    console.log('🔍 Finding unverified users...\n');
    
    const unverifiedUsers = await prismaAuth.user.findMany({
      where: {
        emailVerified: false,
      },
      select: {
        id: true,
        email: true,
        name: true,
        createdAt: true,
      },
      orderBy: {
        createdAt: 'desc',
      },
    });
    
    if (unverifiedUsers.length === 0) {
      console.log('✅ No unverified users found.');
      return;
    }
    
    console.log(`Found ${unverifiedUsers.length} unverified user(s):\n`);
    unverifiedUsers.forEach((user, index) => {
      console.log(`${index + 1}. ${user.email} (${user.name}) - Created: ${user.createdAt}`);
    });
    
    // Verify all unverified users
    console.log('\n✅ Verifying all unverified users...\n');
    
    for (const user of unverifiedUsers) {
      await prismaAuth.user.update({
        where: { id: user.id },
        data: {
          emailVerified: true,
          emailConfirmationToken: null,
          emailConfirmationTokenExpiry: null,
        },
      });
      console.log(`✅ Verified: ${user.email} (${user.name})`);
    }
    
    console.log('\n🎉 All users have been verified! They can now log in.');
    
  } catch (error: any) {
    console.error('❌ Error:', error.message);
    if (error.message?.includes('email_verified')) {
      console.error('\n⚠️  Database migration may not have been run!');
      console.error('  Run: sudo -u postgres psql -d punimtag_auth -f migrations/add-email-verification-columns.sql');
    }
    process.exit(1);
  } finally {
    await prismaAuth.$disconnect();
  }
}

manuallyVerifyUser();







