const fetch = require('node-fetch-commonjs')

exports.handler = async function (event) {
    if (event.body === null) {
      return {
        statusCode: 400,
        body: JSON.stringify("Payload required"),
      };
    }
  
    const requestBody = JSON.parse(event.body)

    //automatically generated snippet from the email preview
    //sends a request to an email handler for a subscribed email
    await fetch(`${process.env.URL}/.netlify/functions/emails/BookNow`, {
        headers: {
          "netlify-emails-secret": process.env.NEXT_PUBLIC_NETLIFY_EMAILS_SECRET,
        },
        method: "POST",
        body: JSON.stringify({
            from: process.env.NEXT_PUBLIC_NETLIFY_FROM_EMAIL,
            to: requestBody.subscriberEmail,
            subject: "Thanks For Booking on Sign365",
            parameters: {
                email: requestBody.subscriberEmail,
            },
        }),
      }
    );
  
    return {
      statusCode: 200,
      body: JSON.stringify("SignUp email sent!"),
    };
}