const UnitOfWork = require('../database/UnitOfWork');
const argon2 = require('argon2');

async function createPersonalUser() {
  return;
  // Edit these values directly in code (no env vars)
  const personalEmail = 'dummy-personal@profitplanet.local';
  const personalPassword = 'dummyPass!1234';
  const firstName = 'Dummy';
  const lastName = 'User';
  const phone = '+490000000003';

  const uow = new UnitOfWork();
  await uow.start();
  try {
    const [existing] = await uow.connection.query(
      `SELECT id FROM users WHERE email = ? AND user_type = 'personal' LIMIT 1`,
      [personalEmail]
    );

    const hashed = await argon2.hash(personalPassword);
    let userId;

    if (existing.length) {
      userId = existing[0].id;
      await uow.connection.query(
        `UPDATE users SET password = ? WHERE id = ?`,
        [hashed, userId]
      );
      console.log('✅ Personal user password updated');
    } else {
      // Create base user record
      const [userRes] = await uow.connection.query(
        `INSERT INTO users (email, password, user_type, role, created_at) VALUES (?, ?, 'personal', 'user', NOW())`,
        [personalEmail, hashed]
      );
      userId = userRes.insertId;

      // Create personal profile
      await uow.connection.query(
        `INSERT INTO personal_profiles (user_id, first_name, last_name, phone)
         VALUES (?, ?, ?, ?)`,
        [userId, firstName, lastName, phone]
      );

      // Initialize user status
      await uow.connection.query(
        `INSERT INTO user_status (user_id, status, is_admin_verified, email_verified, profile_completed, documents_uploaded, contract_signed)
         VALUES (?, 'pending', 0, 0, 0, 0, 0)`,
        [userId]
      );

      // Initialize user settings
      await uow.connection.query(
        `INSERT INTO user_settings (user_id) VALUES (?)`,
        [userId]
      );

      console.log('✅ Personal user created and initialized');
    }

    await uow.commit();
    return { ok: true, userId, email: personalEmail };
  } catch (error) {
    await uow.rollback(error);
    console.error('💥 Failed to create personal user:', error);
    throw error;
  }
}

module.exports = createPersonalUser;
