import readline from "node:readline";
import os from "os";

let isPrompting = false; // prevent multiple prompts at the same time

export async function askOtp() {
  console.log("ASKING OTP");
  if (isPrompting) return null; // prevent multiple prompts
  isPrompting = true;
  console.log("is not prompting");

  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
  });

  const question = (query) =>
    new Promise((resolve) => rl.question(query, resolve));

  try {
    console.log("-----------");
    const enteredOtp = await question("Enter OTP: ");
    console.log("-----------");
    isPrompting = false;
    rl.close();
    return enteredOtp.trim();
  } catch (error) {
    console.log("Error");
  }
}

export function notPrompting() {
  isPrompting = false;
}

export function getDeviceInfo() {
  return {
    os: {
      platform: os.platform(),
      type: os.type(),
      release: os.release(),
      arch: os.arch(),
      hostname: os.hostname(),
    },
    cpu: {
      cores: os.cpus().length,
      model: os.cpus()[0].model,
      speedMHz: os.cpus()[0].speed,
    },
    user: os.userInfo(),
    process: {
      nodeVersion: process.version,
      execPath: process.execPath,
    },
  };
}
