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

async function initDefaultUser() {
  try {
    const [users] = await pool.query('SELECT userId FROM User WHERE username = ?', ['admin']);
    if (users.length === 0) {
      const hashedPassword = await bcrypt.hash('admin123', 10);
      await pool.query('INSERT INTO User (username, password) VALUES (?, ?)', ['admin', hashedPassword]);
      console.log('Default user created: username=admin, password=admin123');
    }
  } catch (error) {
    console.warn('Could not initialize default user:', error.message);
  }
}

module.exports = initDefaultUser;
