import { logger } from "@/lib/logger";

const DEFAULT_FROM_EMAIL = "noreply@mawidi.com";
const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

export function getFromEmail(): string {
  const configured = process.env.SMTP_FROM;

  if (configured && EMAIL_REGEX.test(configured)) {
    return configured;
  }

  if (configured && !EMAIL_REGEX.test(configured)) {
    logger.warn("SMTP_FROM is set but invalid, falling back to default", {
      configured,
    });
  } else {
    logger.info("SMTP_FROM not set, using default sender address", {
      default: DEFAULT_FROM_EMAIL,
    });
  }

  if (process.env.NODE_ENV === "production") {
    logger.error(
      "SMTP_FROM must be set to a valid email address in production",
    );
  }

  return DEFAULT_FROM_EMAIL;
}
