const mysql = require('mysql2/promise');
const bcrypt = require('bcryptjs');

async function updateSuperAdminPassword() {
  try {
    const connection = await mysql.createConnection({
      host: 'localhost',
      user: 'root',
      password: '',
      database: 'cms_db'
    });

    console.log('🔧 Updating Super Admin password...');
    
    // Get Super Admin user
    const [users] = await connection.execute('SELECT * FROM users WHERE role_id = 1 LIMIT 1');
    if (users.length === 0) {
      console.log('❌ No Super Admin user found');
      return;
    }

    const superAdmin = users[0];
    console.log('✅ Super Admin user found:', superAdmin.name);

    // Hash the password
    const hashedPassword = await bcrypt.hash('admin123', 10);
    
    // Update password
    await connection.execute(
      'UPDATE users SET password = ? WHERE id = ?',
      [hashedPassword, superAdmin.id]
    );

    console.log('✅ Super Admin password updated to: admin123');

    // Verify the update
    const [updatedUser] = await connection.execute('SELECT * FROM users WHERE id = ?', [superAdmin.id]);
    console.log('✅ Password updated successfully');

    await connection.end();
  } catch (error) {
    console.error('❌ Error:', error.message);
  }
}

updateSuperAdminPassword();
