const db = require('./config/database');
const bcrypt = require('bcryptjs');

async function fixPasswords() {
  try {
    const hashedPassword = await bcrypt.hash('school123', 10);
    
    // Update both school users
    await db.execute(
      'UPDATE users SET password_hash = ? WHERE email = ? AND user_type = ?',
      [hashedPassword, 'principal@school.com', 'school']
    );
    
    await db.execute(
      'UPDATE users SET password_hash = ? WHERE email = ? AND user_type = ?',
      [hashedPassword, 'school@test.com', 'school']
    );
    
    console.log('Updated passwords for both school users');
    
    // Verify
    const [users] = await db.execute('SELECT email FROM users WHERE user_type = "school"');
    for (const user of users) {
      const [rows] = await db.execute('SELECT password_hash FROM users WHERE email = ?', [user.email]);
      const isValid = await bcrypt.compare('school123', rows[0].password_hash);
      console.log(`${user.email}: password valid = ${isValid}`);
    }
    
    process.exit(0);
  } catch (error) {
    console.error('Error:', error);
    process.exit(1);
  }
}

fixPasswords();