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:', {
      id: superAdmin.id,
      name: superAdmin.name,
      email: superAdmin.email,
      login_id: superAdmin.login_id
    });

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

    console.log('✅ Super Admin password updated to: A12345');
    console.log('✅ User activated');

    // Verify the update
    const [updatedUser] = await connection.execute('SELECT id, name, email, login_id, is_active FROM users WHERE id = ?', [superAdmin.id]);
    console.log('✅ Updated user data:', updatedUser[0]);

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

updateSuperAdminPassword();
