const utils = require('./utils/Utils.js');
const config = require('../config/Config.js');
const uuidV4 = require('uuid/v4');

/** @module aggregate */
/**
 * Takes a conversation and extracts:
 * - facebook messenger id
 * - a conversation metadata object that has metadata and the conversation
 * @param {object} conversation - A conversation object generated by botkit.
 * @return {object} Messenger id and submission metadata from the conversation.
 */
function aggregate(conversation) {
  const {startTime, lastActive, responses, context} = conversation;
  const times = extractTimeStamps(responses);
  const metadata = {
    start: startTime,
    stop: lastActive,
    bot_identifier: context,
    responses: utils.mergeObjects(responses, times) ,
    conversation,
  };

  return {
    userId: context.user,
    submission_metadata: metadata,
  };
}

/**
* @param {object} responses - value of the responses key in the conversation
* object
* @return {array} - An array of objects with question + TimeStamp and timestamps
*/
function extractTimeStamps(responses) {
  return Object.keys(responses)
    .reduce((x, key) => {
      const obj = {};
      obj[key + 'TimeStamp'] = {text: responses[key].timestamp};
      return Object.assign(x, obj);
    }, {});
}

/**
* Generate an ona submission from a completed conversation
* @param {object} conversation the conversation with the bot
* @return {object} a valid Ona submission object
*/
function genOnaSubmission(conversation) {
  const submission = {};
  submission.id = config.onaFormIdString;
  const responses = conversation.submission_metadata.responses;
  const names = Object.keys(responses);

  const subb = names.map((name) => {
    const kv = {};
    if (name === 'repeat') {
      kv.spoken = responses.repeat.spoken;
      kv.with_whom = responses.repeat.with_whom;
      return kv;
    }
    kv[name] = responses[name].text;
    return kv;
  });

  const sub = subb.reduce((x = {}, kv) => Object.assign(x, kv));
  submission.submission = sub;
  submission.submission.meta = {instanceID: `uuid:${uuidV4()}`};
  return submission;
}

module.exports = {
  aggregate,
  genOnaSubmission
};

@@ -1,12 +1,38 @@
+/** @module aggregate */%0A%0A
 const utils 
@@ -149,33 +149,8 @@
 );%0A%0A
-/** @module aggregate */%0A
 /**%0A
@@ -744,17 +744,16 @@
 , times)
- 
 ,%0A    co
@@ -955,50 +955,94 @@
 rn %7B
-array
+object
 %7D - An 
-array of objects with 
+object of question and ISO 8601 timestamps with the%0A* structure %7B
 question
  + T
@@ -1037,19 +1037,18 @@
 question
- + 
++'
 TimeStam
@@ -1052,23 +1052,43 @@
 tamp
- and timestamps
+': %7Btext: %3Ctimestamp in ISO 8601%3E%7D%7D
 %0A*/%0A
@@ -1172,17 +1172,21 @@
 reduce((
-x
+accum
 , key) =
@@ -1207,24 +1207,79 @@
 t obj = %7B%7D;%0A
+      const date = new Date(responses%5Bkey%5D.timestamp);%0A
       obj%5Bke
@@ -1308,32 +1308,12 @@
 xt: 
-responses%5Bkey%5D.timestamp
+date
 %7D;%0A 
@@ -1338,17 +1338,21 @@
 .assign(
-x
+accum
 , obj);%0A
@@ -2250,12 +2250,13 @@
 bmission
+,
 %0A%7D;%0A

Use ISO date format