const _S = require('underscore.string');
const moment = require('moment');
const { fillInInteractiveMessage } = require('./interactive-message');

const makeReviewAttachment = (review, ...partBuilders) => {
  const stars = _S.repeat('★', review.stars) + _S.repeat('✩', 5 - review.stars);
  const reviewMoment = moment(review.createdAt);
  const color = (review.stars >= 4) ? 'good' : (review.stars <= 2) ? 'danger' : 'warning';
  const basicAttachment = {
    'author_name': review.consumer.displayName,
    'title': review.title,
    'text': review.text,
    'color': color,
    'footer': stars,
    'ts': reviewMoment.format('X'),
  };

  return partBuilders.reduce((attachment, builder) => {
    return { ...attachment, ...builder(review) };
  }, basicAttachment);
};

const actionsPartBuilder = (actionsMap) => (review) => {
  const actions = [...actionsMap].filter(([, isPermitted]) => isPermitted).map(([action]) => action);
  return actions.length ? {
    'callback_id': review.id,
    actions,
  } : {};
};

const replyAction = {
  'value': 'step_1_write_reply',
  'text': ':writing_hand: Reply',
};

const composeReviewMessage = (review, { canReply }) => {
  const actionsMap = new Map();
  actionsMap.set(replyAction, canReply);

  return fillInInteractiveMessage({
    'attachments': [
      makeReviewAttachment(review, actionsPartBuilder(actionsMap)),
    ],
  });
};

module.exports = { composeReviewMessage };

@@ -626,16 +626,152 @@
 t('X'),%0A
+    'fields': %5B%7B%0A      title: 'Source',%0A      value: review.referenceId ? %60Reference number $%7Breview.referenceId%7D%60 : 'Organic',%0A    %7D%5D,%0A
   %7D;%0A%0A  

Add source of review