// server/helpers/email.js
import * as config from "../config.js";

const style = `
    background: #eee;
    padding: 20px;
    border-radius: 20px;
`;

export const emailTemplate = (email, content, replyTo, subject) => {
  return {
    Source: config.EMAIL_FROM,
    ReplyToAddresses: [replyTo],
    Destination: {
      ToAddresses: [email],
    },
    Message: {
      Body: {
        Html: {
          Charset: "UTF-8",
          Data: `
            <html>
              <div style="${style}">
                <h1>Welcome to Realist App</h1>
                ${content}
                <p>&copy; ${new Date().getFullYear()}</p>
              <div>
          </html>    
                    `,
        },
      },
      Subject: {
        Charset: "UTF-8",
        Data: subject,
      },
    },
  };
};

Now use in auth controller

import { emailTemplate } from "../helpers/email.js";

export const preRegister = async (req, res) => {
  try {
    const { email, password } = req.body;
    // generate jwt using email and password
    const token = jwt.sign({ email, password }, config.JWT_SECRET, {
      expiresIn: "1h",
    });
    // send test email
    config.AWSSES.sendEmail(
      emailTemplate(
        email,
        `
        <p>Please click the link below to activate your account.</p>
        <a href="${config.CLIENT_URL}/auth/account-activate/${token}">Activate my account</a>
    `,
        config.REPLY_TO,
        "Welcome to Realist app"
      ),
      (err, data) => {
        if (err) {
          return res.json({ error: "Provide a valid email address" });
        } else {
          return res.json({ error: "Check email to complete registration" });
        }
      }
    );
  } catch (err) {
    console.log(err);
    res.json({ error: "Something went wrong. Try again." });
  }
};