import nodemailer from "nodemailer";
import fs from "fs";
import path from "path";

export const sendForgetEmail = async (to, subject, message, userName) => {
  try {
    const templatePath = path.resolve("./templates/forgetEmail.html");
    let htmlTemplate = fs.readFileSync(templatePath, "utf8");

    // Replace placeholder
    const replacements = {
      "{{firstName}}": userName || "there",
      "{{message}}": message || "",
    };
   
for(const placeholder in replacements){
 htmlTemplate = htmlTemplate.replace(placeholder,replacements[placeholder]);
}


    const transporter = nodemailer.createTransport({
      host: "smtp.gmail.com",
      secure: true,
      auth: {
        user: process.env.GOOGLE_APP_EMAIL,
        pass: process.env.GOOGLE_APP_PASSCODE, // ⚠️ REPLACE with 16-character app password from Google
      },
    });

    await transporter.sendMail({
      from: process.env.GOOGLE_APP_EMAIL,
      to: to,
      subject: subject,
      html:htmlTemplate, // HTML body
    });
    console.log("Message sent:", to);
  } catch (err) {
    console.log("Message err:", err);
  }
};
