require("dotenv").config();

const {
  sendLowBalanceAlert,
  sendAutomationFailureAlert,
  sendCriticalErrorAlert,
  sendSessionExpiredAlert,
} = require("./services/emailService");

async function runTest() {
  const kind = process.argv[3] || "low-balance";

  if (kind === "low-balance") {
    return sendLowBalanceAlert(45000, "MMK");
  }
  if (kind === "automation-failure") {
    return sendAutomationFailureAlert("Purchase timeout", {
      orderId: "ORD-TEST",
      product: "86 Diamonds",
    });
  }
  if (kind === "critical-error") {
    return sendCriticalErrorAlert(new Error("Browser crashed"), {
      component: "browser",
    });
  }
  if (kind === "session-expired") {
    return sendSessionExpiredAlert("Smile.one main account");
  }

  throw new Error(`Unknown test type: ${kind}`);
}

if (require.main === module) {
  if (process.argv[2] !== "test") {
    console.log("Email notification module is ready.");
    console.log("Run `npm run test:email` after creating notification/.env.");
    process.exit(0);
  }

  runTest()
    .then((result) => {
      console.log(JSON.stringify(result, null, 2));
    })
    .catch((error) => {
      console.error(error.message);
      process.exit(1);
    });
}
