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: "mail.cssawwwards.com",
      port: 465,
      secure: true,
      auth: {
        user: process.env.CPANEL_EMAIL || "contact@cssawwwards.com",
        pass: process.env.CPANEL_EMAIL_PASSWORD,
      },
      tls: {
        rejectUnauthorized: false // Bypass SSL certificate validation if needed
      },
      debug: true, // Enable debugging
      logger: true // Log output
    });

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