// config.js
export const CLIENT_URL = "http://localhost:3000";
export const JWT_SECRET = "XXXXXXXXXXXX";

// controllers/auth
import * as config from "../config.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(
      {
        Source: config.EMAIL_FROM,
        ReplyToAddresses: [config.REPLY_TO],
        Destination: {
          ToAddresses: [email],
        },
        Message: {
          Body: {
            Html: {
              Charset: "UTF-8",
              Data: `
              <html>
              <h1>Welcome to Realist App</h1>
              <p>Please click the button below to activate your account.</p>
              <a href="${config.CLIENT_URL}/auth/account-activate/${token}" target="_blank">Activate my account</a>
          </html>    
                    `,
            },
          },
          Subject: {
            Charset: "UTF-8",
            Data: "Welcome to Realist",
          },
        },
      },
      (err, data) => {
        if (err) {
          console.log("Provide a valid email address", err);
          return res.json({ ok: false });
        } else {
          console.log("Check email to complete registration", data);
          return res.json({ ok: true });
        }
      }
    );
  } catch (err) {
    console.log(err);
  }
};