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

async function setupAllUsers() {
  try {
    console.log('Setting up all test users...');
    
    // Hash passwords
    const schoolPassword = await bcrypt.hash('school123', 10);
    const adminPassword = await bcrypt.hash('admin123', 10);
    const parentPassword = await bcrypt.hash('password123', 10);
    
    // Create/Update school user
    await db.execute(`
      INSERT INTO users (id, username, email, password_hash, first_name, last_name, user_type, phone) 
      VALUES (100, 'schooladmin', 'school@test.com', ?, 'School', 'Admin', 'school', '555-0123')
      ON DUPLICATE KEY UPDATE 
      password_hash = VALUES(password_hash),
      first_name = VALUES(first_name),
      last_name = VALUES(last_name)
    `, [schoolPassword]);
    console.log('✓ School user: school@test.com / school123');
    
    // Create/Update admin user
    await db.execute(`
      INSERT INTO users (id, username, email, password_hash, first_name, last_name, user_type, phone) 
      VALUES (101, 'superadmin', 'admin@gpstracking.com', ?, 'Super', 'Admin', 'admin', '555-0124')
      ON DUPLICATE KEY UPDATE 
      password_hash = VALUES(password_hash),
      first_name = VALUES(first_name),
      last_name = VALUES(last_name)
    `, [adminPassword]);
    console.log('✓ Admin user: admin@gpstracking.com / admin123');
    
    // Create/Update parent user
    await db.execute(`
      INSERT INTO users (id, username, email, password_hash, first_name, last_name, user_type, phone) 
      VALUES (102, 'testparent', 'parent@email.com', ?, 'Test', 'Parent', 'parent', '555-0125')
      ON DUPLICATE KEY UPDATE 
      password_hash = VALUES(password_hash),
      first_name = VALUES(first_name),
      last_name = VALUES(last_name)
    `, [parentPassword]);
    console.log('✓ Parent user: parent@email.com / password123');
    
    console.log('\nAll users setup complete!');
    console.log('You can now login with any of the above credentials.');
    
  } catch (error) {
    console.error('Error setting up users:', error);
  } finally {
    process.exit();
  }
}

setupAllUsers();