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

async function sendEmail(emailData) {
  db.getConnection((err, connection) => {
    if (err) {
      console.log("database not connected error: >>>", err);
      throw err;
    }
    console.log("get Connection");
    let query = `Select userEmails from finehhdi_eDefect.UsersPriority where priority = '${emailData.hfound}'`
    console.log('EmailData', emailData)

    connection.query(query, async (err, data) => {
      connection.release()
      console.log('if user is not',data.length)
      if(data.length==0){
        return
      }
      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.finehhdigital.com",
          port: 465,
          secure: true, // true for 465, false for other ports
          auth: {
            user: 'edefect@finehhdigital.com', // generated ethereal user
            pass: 'C%1JSw8-jfF]', // generated ethereal password
          },
        });
        // send mail with defined transport object
        let info = await transporter.sendMail({
          from: 'edefect@finehhdigital.com', // 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...
      }
    })
    console.log("release Connection");
  });
}

module.exports = {
  sendEmail,
};