text: const nodemailer = require('nodemailer');
  const pug = require('pug');
  const { convert } = require('html-to-text');
  
  module.exports = class Email {
    constructor(user, url) {
      this.to = user.email;
      this.firstName = user.name.split(' ')[0];
      this.url = url;
      this.from = `Mark Njoroge <${process.env.EMAIL_FROM}>`;
    }
  
    newTransport() {
      // prod vs Dev
      if (process.env.NODE_ENV === 'production') {
        // Sendgrind
  
        return nodemailer.createTransport({
          service: 'SendGrid',
          auth: {
            user: process.env.SENDGRID_USERNAME,
            pass: process.env.SENDGRID_PASSWORD,
          },
        });
      }
  
      // Development: Send to Mailtrap
      return nodemailer.createTransport({
        host: process.env.EMAIL_HOST,
        port: process.env.EMAIL_PORT,
        auth: {
          user: process.env.EMAIL_USERNAME,
          pass: process.env.EMAIL_PASSWORD,
        },
  
        // Activate in gmail 'less secure app" option
      });
    }
  
    async send(template, subject) {
      // Send the actual email
      // 1) Render HTML based on a pug template
      const html = pug.renderFile(
        `${__dirname}/../views/emails/${template}.pug`,
        {
          firstName: this.firstName,
          url: this.url,
          subject,
        }
      );
  
      // 2) Define the email options
      const mailOptions = {
        from: this.from,
        to: this.to,
        subject: subject,
        html,
        text: convert(html, {
          wordwrap: 80,
          preserveNewlines: true,
        }),
      };
  
      // 3) Create a transport and send email
      this.newTransport();
      // await transporter.sendMail(mailOptions);
      await this.newTransport().sendMail(mailOptions);
    }
  
    async sendWelcome() {
      await this.send('welcome', 'Welcome to the Natours Family!');
    }
  
    async sendPasswordReset() {
      await this.send(
        'passwordResetEmail',
        'Here is your password reset token (It is valid for 10 min)'
      );
    }
  };
  
  // const sendEmail = async options => {
  //   // // 1) Create a transporter
  //   // const transporter = nodemailer.createTransport({
  //   //   host: process.env.EMAIL_HOST,
  //   //   port: process.env.EMAIL_PORT,
  //   //   auth: {
  //   //     user: process.env.EMAIL_USERNAME,
  //   //     pass: process.env.EMAIL_PASSWORD,
  //   //   },
  //   //   // Activate in gmail 'less secure app" option
  //   // });
  //   // // 2) Define the email options
  //   // const mailOptions = {
  //   //   from: 'Mark Njoroge <hello@marknjo.io>',
  //   //   to: options.email,
  //   //   subject: options.subject,
  //   //   text: options.message,
  //   // };
  //   // // 3) Send email with the node mailer
  //   // await transporter.sendMail(mailOptions);
  // };
  
  // module.exports = sendEmail;
  