var express = require('express')
const db = require("../database");
const nodemailer = require("nodemailer");

async function sendEmail(emailData) {
  console.log('emailImage',emailData.image)
  let query = `Select userEmails from UsersPriority where priority = '${emailData.priority}'`
  console.log('EmailData', emailData)
  db.query(query, async (err, data) => {
    if (err) { console.log(err) }
    else {
      console.log(data.map(item => item.userEmails).join())
      // Generate test SMTP service account from ethereal.email
      // Only needed if you don't have a real mail account for testing

      // create reusable transporter object using the default SMTP transport
      let transporter = nodemailer.createTransport({
        host: "mail.pakistanautomation.com.pk",
        port: 465,
        secure: true, // true for 465, false for other ports
        auth: {
          user: 'defecthandling@pakistanautomation.com.pk', // generated ethereal user
          pass: 'o&T*U(Eibn7A', // generated ethereal password
        },
      });
      // send mail with defined transport object
      let info = await transporter.sendMail({
        from: 'defecthandling@pakistanautomation.com.pk', // sender address
        to: `${data.map(item => item.userEmails).join()}`, // list of receivers
        subject: `Defect Logged | Priority:${emailData.priority}`, // Subject line
        text: `Hello world?text${emailData.priority}`, // plain text body
        html: `
        <table style="font-family: arial, sans-serif;
            border-collapse: collapse;
            width: 100%;">
                <tr>
                        <th style=" border: 1px solid #dddddd;
                  text-align: left;
                  padding: 8px;">Area</th>
                        <th style=" border: 1px solid #dddddd;
                  text-align: left;
                  padding: 8px;">Machine</th>
                  <th style=" border: 1px solid #dddddd;
                  text-align: left;
                  padding: 8px;">Owner</th>
                        <th style=" border: 1px solid #dddddd;
                  text-align: left;
                  padding: 8px;">System</th>
                  <th style=" border: 1px solid #dddddd;
                  text-align: left;
                  padding: 8px;">Founder</th>
                  <th style=" border: 1px solid #dddddd;
                  text-align: left;
                  padding: 8px;">Responsible</th>
                  <th style=" border: 1px solid #dddddd;
                  text-align: left;
                  padding: 8px;">Defect</th>
                  <th style=" border: 1px solid #dddddd;
                  text-align: left;
                  padding: 8px;">Repeated Defect</th>
                  <th style=" border: 1px solid #dddddd;
                  text-align: left;
                  padding: 8px;">Defect Part</th>
                  <th style=" border: 1px solid #dddddd;
                  text-align: left;
                  padding: 8px;">Date Founded</th>
                  <th style=" border: 1px solid #dddddd;
                  text-align: left;
                  padding: 8px;">Created On</th>
                  <th style=" border: 1px solid #dddddd;
                  text-align: left;
                  padding: 8px;">Action Plan</th>
                   <th style=" border: 1px solid #dddddd;
                   text-align: left;
                   padding: 8px;">Defect Detail</th>
                </tr>
                <tr>
                        <td style="
                   border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;">${emailData.department}</td>
                        <td style="
                   border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;">${emailData.machine}</td>
  <td style="
  border: 1px solid #dddddd;
text-align: left;
padding: 8px;">${emailData.owner}</td>
                        <td style="
                   border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;">${emailData.hfound}</td>
   <td style="
   border: 1px solid #dddddd;
text-align: left;
padding: 8px;">${emailData.wfound}</td>
 <td style="
 border: 1px solid #dddddd;
text-align: left;
padding: 8px;"> ${emailData.responsible}</td>
 <td style="
 border: 1px solid #dddddd;
text-align: left;
padding: 8px;">${emailData.anamoly}</td>
 <td style="
 border: 1px solid #dddddd;
text-align: left;
padding: 8px;">${emailData.repeatedDefect}</td>
 <td style="
 border: 1px solid #dddddd;
text-align: left;
padding: 8px;">${emailData.defectPart}</td>
 <td style="
 border: 1px solid #dddddd;
text-align: left;
padding: 8px;">${emailData.targetDate}</td>
 <td style="
 border: 1px solid #dddddd;
text-align: left;
padding: 8px;">${emailData.createdOn}</td>
 <td style="
 border: 1px solid #dddddd;
text-align: left;
padding: 8px;">${emailData.actionPlan}</td>
 <td style="
 border: 1px solid #dddddd;
text-align: left;
padding: 8px;">${emailData.anamolyDetail}</td>
                </tr>
               
        </table>
        `
        , // html body
        attachments: emailData.image ==='No Image' ?  [] :  [{ filename: 'image.jpeg', path: emailData.image }] 
      })

      console.log("Message sent: %s", info.messageId);
      // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>

      // Preview only available when sending through an Ethereal account
      console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
      // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
    }
  })
}

module.exports = {
  sendEmail,
};