import nodemailer from "nodemailer";

function getRequiredEnv(name: string) {
  const value = process.env[name];
  if (!value) {
    throw new Error(`Missing required environment variable: ${name}`);
  }
  return value;
}

export function createMailerTransport() {
  return nodemailer.createTransport({
    host: getRequiredEnv("SMTP_HOST"),
    port: Number(process.env.SMTP_PORT || 465),
    secure: true,
    auth: {
      user: getRequiredEnv("SMTP_USER"),
      pass: getRequiredEnv("SMTP_PASS"),
    },
  });
}

export function getMailerConfig() {
  return {
    from: process.env.CONTACT_FROM_EMAIL || getRequiredEnv("SMTP_USER"),
    to: process.env.CONTACT_TO_EMAIL || "info@bizone.id",
  };
}
