import { env } from "../src/config/env.ts";
import { isMailConfigured, sendMail, verifyMailTransport } from "../src/lib/mail.ts";

const to = process.argv[2]?.trim();

if (!to) {
  console.error("Usage: npm run smtp:test -- <recipient@example.com>");
  process.exit(1);
}

if (!isMailConfigured()) {
  console.error("SMTP is not configured.");
  console.error({
    host: env.SMTP_HOST,
    port: env.SMTP_PORT,
    secure: env.SMTP_SECURE,
    user: env.SMTP_USER,
    from: env.SMTP_FROM_EMAIL
  });
  process.exit(1);
}

console.log("Verifying SMTP transport...", {
  host: env.SMTP_HOST,
  port: env.SMTP_PORT,
  secure: env.SMTP_SECURE,
  user: env.SMTP_USER,
  from: env.SMTP_FROM_EMAIL,
  to
});

await verifyMailTransport();
console.log("SMTP transport verified.");

await sendMail({
  to,
  subject: "Alpinbet SMTP test",
  text: "This is a test email from the Alpinbet backend.",
  html: "<p>This is a test email from the Alpinbet backend.</p>"
});

console.log("SMTP test email sent.");
