const nodemailer = require("nodemailer");

// const transporter = nodemailer.createTransport({
//   host: "mail.techsolutionfactory.com",
//   port: 465,
//   secure: true,
//   auth: {
//     user: "scb@techsolutionfactory.com",
//     pass: "lmgf~xTS+5pP",
//   },
// });

// const transporter = nodemailer.createTransport({
//   service: "gmail",
//   host: "smtp.gmail.com",
//   port: 587,
//   secure: false,
//   auth: {
//     user: "user",
//     pass: "pass",
//   },
// });

const transporter = nodemailer.createTransport({
  host: "mail.techsolutionfactory.com",
  port: 465,
  secure: true,
  auth: {
    user: process.env.SMTP_USER,
    pass: process.env.SMTP_PASS,
  },
});

// function jsona() {
//   let obj = {
//     api_key: "48qwe84sdfstrwernbm8gf4f5644zxc4534r_)(#4rdsf",
//     send_addr: "abirwebserver@gmail.com",
//     cc: "escape",
//     bcc: "escape",
//     subject: "New message received from System #4124",
//     body: "Hello Abir!\nWe are Checking the system \n Regards \n TSF",
//     mail_server: "techsolutionfactory.com",
//     username: "scb@techsolutionfactory.com",
//     password: "lmgf~xTS+5pP",
//     from_name: "Md. Abir hossain",
//   };
//   let a = JSON.stringify(obj);
//   var xhr = new XMLHttpRequest();
//   xhr.open("POST", "https://techsolutionfactory.com/mail/check.php", true);
//   xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//   xhr.onload = function () {
//     // do something to response
//     console.log(this.responseText);
//   };
//   xhr.send(`obj=${a}`);
// }

const sendSubConfirmation = async (email) => {
  try {
    const info = await transporter.sendMail({
      from: '"Shippers Council Bangladesh" <scb@techsolutionfactory.com>', // sender address
      to: email, // list of receivers
      subject: "Subscription Confirmed - Welcome to SCB", // Subject line
      // text: "Hello world?", // plain text body
      html: "<p>Greetings from Shippers' Council Bangladesh!</p><p>We are thrilled to confirm your subscription to our newsletter. Your interest is highly valued, and we appreciate you joining our community.</p><p>From now on, expect timely updates and exciting news delivered directly to your inbox. We are committed to providing you with valuable insights and ensuring that our communications enhance your experience with Shippers' Council Bangladesh.</p><p>If you ever need assistance or have questions, feel free to reach out to our support team at scb@techsolutionfactory.com.</p><br/><p>Best regards,</p><p>Shippers' Council Bangladesh.</p>", // html body
    });

    console.log("Message sent: %s", info.messageId);
  } catch (error) {
    console.log("send mail error - ", error);
  }

  // transporter.verify(function (error, success) {
  //   if (error) {
  //     console.log("HERE - ", error);
  //   } else {
  //     console.log("Server is ready to take our messages");
  //   }
  // });
};

const sendContactMessage = async ({ name, number, email, message }) => {
  try {
    const info = await transporter.sendMail({
      from: '"Shippers Council Bangladesh" <scb@techsolutionfactory.com>', // sender address
      to: "temp.ovee@gmail.com", // list of receivers
      subject: "A new message for SCB", // Subject line
      // text: "Hello world?", // plain text body
      html: `<p><b>Greetings, you have recieved a new message from the contact form of the website.</b></p><br/><p><b>From - </b></p><p>Name: ${name}</p><p>Email: ${email}</p><p>Phone: ${number}</p><p>Message: ${message}</p><br/><p>Best regards.</p>`, // html body
    });
    console.log("Message sent to SCB: %s", info.messageId);
    // return info.messageId;

    const info_user = await transporter.sendMail({
      from: '"Shippers Council Bangladesh" <scb@techsolutionfactory.com>', // sender address
      to: email, // list of receivers
      subject: "Inquiry Received - SCB", // Subject line
      // text: "Hello world?", // plain text body
      html: `<p><b>Dear ${name},</b></p><br/><p>Thank you for reaching out to Shippers' Council Bangladesh. We have received your inquiry, and our team is dedicated to addressing your concerns promptly.</p><p>We appreciate your interest in our association, and rest assured that we are committed to providing you with the information or assistance you require. Our team will review your message and respond as soon as possible.</p><br/><p>Best regards,</p><p>Shippers' Council Bangladesh</p>`, // html body
    });
    console.log("Message sent to USER: %s", info_user.messageId);

    let info_obj = {
      info: info.messageId,
      info_user: info_user.messageId,
    };

    return info_obj;
  } catch (error) {
    console.log("send mail error - ", error);
    throw new Error();
  }
};

module.exports = { sendSubConfirmation, sendContactMessage };
