// Test password verification
const database = require('./server/config/database');
const bcrypt = require('bcryptjs');

async function testPassword() {
  try {
    await database.initialize();
    
    const user = await database.get("SELECT * FROM users WHERE email='admin@ashtransfer.com'");
    
    if (!user) {
      console.log('❌ User not found!');
      process.exit(1);
    }
    
    console.log('\n👤 User found:');
    console.log('Email:', user.email);
    console.log('Password hash:', user.password);
    
    // Test password
    const testPassword = 'Global2026@';
    const isValid = await bcrypt.compare(testPassword, user.password);
    
    console.log('\n🔐 Password Test:');
    console.log('Testing password:', testPassword);
    console.log('Result:', isValid ? '✅ VALID' : '❌ INVALID');
    
    if (!isValid) {
      console.log('\n🔧 Fixing password...');
      const newHash = await bcrypt.hash(testPassword, 10);
      await database.run(
        "UPDATE users SET password=? WHERE email='admin@ashtransfer.com'",
        [newHash]
      );
      console.log('✅ Password updated!');
      
      // Verify again
      const updatedUser = await database.get("SELECT * FROM users WHERE email='admin@ashtransfer.com'");
      const isValidNow = await bcrypt.compare(testPassword, updatedUser.password);
      console.log('Verification:', isValidNow ? '✅ NOW VALID' : '❌ STILL INVALID');
    }
    
    await database.close();
    process.exit(0);
  } catch (error) {
    console.error('❌ Error:', error);
    process.exit(1);
  }
}

testPassword();
