text: const nodemailer = require('nodemailer');
  const pug = require('pug');
  const htmlToText = require('html-to-text');
  
  module.exports = class Email {
    constructor(user, otp) {
      this.to = user.email;
      this.firstName = user.name.split(' ')[0];
      this.from = `<${process.env.EMAIL_FROM}>`;
      this.otp = otp;
    }
  
    newTransport() {
      if (process.env.NODE_ENV === 'production') {
        //sendgrid
        return nodemailer.createTransport({
          service: 'SendGrid',
          auth: {
            user: process.env.SENDGRID_USERNAME,
            pass: process.env.SENDGRID_PASSWORD,
          },
        });
      }
  
      return nodemailer.createTransport({
        host: process.env.EMAIL_HOST,
        port: process.env.EMAIL_PORT,
        auth: {
          user: process.env.EMAIL_USER,
          pass: process.env.EMAIL_PASSWORD,
        },
      });
    }
  
    async send(template, subject, otp) {
      //1)
  
      const html = pug.renderFile(`${__dirname}/../views/email/${template}.pug`, {
        email: this.to,
        firstName: this.firstName,
        subject,
        otp,
      });
  
      //2)
      const mailOptions = {
        from: this.from,
        to: this.to,
        subject,
        html,
        text: htmlToText.fromString(html),
      };
  
      //3)
      await this.newTransport().sendMail(mailOptions);
    }
  
    async sendWelcome() {
      await this.send('welcome', 'Welcome to the gamerr family', this.otp);
    }
  
    async sendPasswordReset() {
      await this.send(
        'passwordReset',
        'Your password reset token (valid for only 10 min)'
      );
    }
  };
  