const bcrypt = require('bcryptjs');
const mysql = require('mysql2/promise');
require('dotenv').config({ path: require('path').join(__dirname, '..', '.env') });

async function seed() {
  const connection = await mysql.createConnection({
    host: process.env.DB_HOST || 'localhost',
    user: process.env.DB_USER || 'root',
    password: process.env.DB_PASSWORD || '',
    database: process.env.DB_NAME || 'hms_kfh_rwanda',
  });

  const users = [
    {
      full_name: 'Marie Uwase',
      email: 'reception@kfh.rw',
      password: 'receptionist123',
      role: 'receptionist',
    },
    {
      full_name: 'Dr. Jean Pierre Nkurunziza',
      email: 'doctor1@kfh.rw',
      password: 'doctor123',
      role: 'doctor',
    },
    {
      full_name: 'Dr. Alice Mukamana',
      email: 'doctor2@kfh.rw',
      password: 'doctor123',
      role: 'doctor',
    },
  ];

  console.log('Seeding users...');
  for (const user of users) {
    const hashed = await bcrypt.hash(user.password, 10);
    await connection.query(
      `INSERT INTO users (full_name, email, password, role)
       VALUES (?, ?, ?, ?)
       ON DUPLICATE KEY UPDATE full_name = VALUES(full_name), password = VALUES(password)`,
      [user.full_name, user.email, hashed, user.role]
    );
    console.log(`  ${user.email} / ${user.password}`);
  }

  await connection.end();
  console.log('Seed complete.');
}

seed().catch((err) => {
  console.error(err);
  process.exit(1);
});
