const https = require('https');
const AWS = require('aws-sdk');

const sns = new AWS.SNS({
  apiVersion: '2010-03-31',
  region: process.env.SLACK_MESSAGE_RELAY_SNS_TOPIC_ARN.split(':')[3],
});

const CLASSY_API_CLIENT_ID = process.env.CLASSY_API_CLIENT_ID;
const CLASSY_API_CLIENT_SECRET = process.env.CLASSY_API_CLIENT_SECRET;
const POLLING_FREQUENCY = +process.env.POLLING_FREQUENCY;

function getAccessToken() {
  return new Promise((resolve, reject) => {
    const body = `grant_type=client_credentials&client_id=${CLASSY_API_CLIENT_ID}&client_secret=${CLASSY_API_CLIENT_SECRET}`;

    const options = {
      host: 'api.classy.org',
      path: `/oauth2/auth`,
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(body),
      },
    };

    const req = https.request(options, (res) => {
      res.setEncoding('utf8');

      let json = '';
      res.on('data', (chunk) => {
        json += chunk;
      });
      res.on('end', () => {
        if (res.statusCode >= 200 && res.statusCode < 300) {
          const payload = JSON.parse(json);
          resolve(payload.access_token);
        } else {
          reject(new Error(`Classy token request failed! ${res.statusCode}`));
        }
      });
    });

    // Generic request error handling
    req.on('error', (e) => reject(e));

    req.write(body);
    req.end();
  });
}

function httpGet(token, path) {
  return new Promise((resolve, reject) => {
    const options = {
      host: 'api.classy.org',
      path: `/2.0${path}`,
      method: 'GET',
      headers: {
        Authorization: `Bearer ${token}`,
      },
    };

    const req = https.get(options, (res) => {
      res.setEncoding('utf8');

      let json = '';
      res.on('data', (chunk) => {
        json += chunk;
      });
      res.on('end', () => {
        try {
          const resPayload = JSON.parse(json);
          resolve(resPayload);
        } catch (e) {
          console.error('Error handling JSON response!');
          reject(e);
        }
      });
    });

    // Generic request error handling
    req.on('error', (e) => reject(e));
  });
}

exports.handler = async (event) => {
  const token = await getAccessToken();

  const payload = await httpGet(token, `/organizations/72482/activity`);

  const now = new Date();
  const threshold = +now - POLLING_FREQUENCY * 60 * 1000;
  // const threshold = +now - 6 * 60 * 1000;

  const mapping = {
    338429: '#radiotopia-donations',
    336384: '#radiotopia-donations',
    330330: '#radiotopia-donations',
    330245: '#radiotopia-donations',
    342678: '#radiotopia-donations',
    326353: '#earhustle-donations',
  };

  for (const activity of payload.data) {
    if (activity.type === 'donation_created') {
      const tx = activity.transaction;
      const camp = activity.campaign;
      const mem = activity.member;

      const ts = Date.parse(activity.created_at);

      if (mapping[camp.id] && ts >= threshold) {
        const comment = tx.comment.length ? `\n> ${tx.comment}` : '';

        // const fullTx = await httpGet(`/transactions/${tx.id}`);
        // console.log(tx);

        let text;
        const name = `${mem?.first_name} ${mem?.last_name.charAt(0)}.`;
        const amt = tx.raw_donation_gross_amount;
        const campUrl = `https://www.classy.org/manage/event/${camp.id}/overview`;
        const txUrl = `https://www.classy.org/admin/72482/transactions/${tx.id}`;
        // const supporterUrl = `https://www.classy.org/admin/72482/supporters/${fullTx.supporter_id}`;

        if (tx.frequency === 'one-time') {
          text = `*${name}* made a <${txUrl}|$${amt}> donation to the <${campUrl}|${camp.name}> campaign${comment}`;
        } else {
          text = `*${name}* created a new ${tx.frequency} recurring giving plan for the <${campUrl}|${camp.name}> campaign for <${txUrl}|$${amt}>${comment}`;
        }

        await sns
          .publish({
            TopicArn: process.env.SLACK_MESSAGE_RELAY_SNS_TOPIC_ARN,
            Message: JSON.stringify({
              channel: mapping[camp.id],
              username: 'Classy',
              icon_emoji: ':classy:',
              text,
            }),
          })
          .promise();
      }
    }
  }
};

@@ -3266,58 +3266,8 @@
 .%60;%0A
-        const amt = tx.raw_donation_gross_amount;%0A
     
@@ -3528,24 +3528,438 @@
 rter_id%7D%60;%0A%0A
+        let amt;%0A        if (tx.currency_code === 'USD') %7B%0A          amt = %60$$%7Btx.raw_donation_gross_amount%7D%60;%0A        %7D else if (tx.currency_code === 'GBP') %7B%0A          amt = %60%C2%A3$%7Btx.raw_donation_gross_amount%7D%60;%0A        %7D else if (tx.currency_code === 'CAD') %7B%0A          amt = %60CA$$%7Btx.raw_donation_gross_amount%7D%60;%0A        %7D else %7B%0A          amt = %60$%7Btx.raw_donation_gross_amount%7D $%7Btx.currency_code%7D%60;%0A        %7D%0A%0A
         if (
@@ -4031,25 +4031,24 @@
  %3C$%7BtxUrl%7D%7C$
-$
 %7Bamt%7D%3E donat
@@ -4260,17 +4260,16 @@
 txUrl%7D%7C$
-$
 %7Bamt%7D%3E$%7B

Improve currency handling