File size: 5,466 Bytes
f4656a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';

/**
 * Formats messages and recommendation offers for WhatsApp/Twilio.
 * WhatsApp supports basic markdown: *bold*, _italic_, ~strikethrough~
 */

/**
 * Format a question for WhatsApp delivery.
 * @param {Object} question - Question object from questionnaire config
 * @param {number} questionNumber - Current question number (1-based)
 * @param {number} totalQuestions - Total questions in flow
 * @param {string} lang - Language code ('en' | 'pt')
 * @returns {string} Formatted message body
 */
function formatQuestion(question, questionNumber, totalQuestions, lang = 'en') {
  const text = question.text[lang] || question.text.en;
  const options = question.options ? question.options[lang] || question.options.en : null;

  let message = `📋 *Question ${questionNumber}/${totalQuestions}*\n\n${text}`;

  if (question.type === 'multiple_choice' && options) {
    message += '\n\n';
    options.forEach((option, idx) => {
      message += `${idx + 1}. ${option}\n`;
    });
    message += '\n_Reply with the number of your choice_';
  } else if (question.type === 'text') {
    const placeholder = question.placeholder ? question.placeholder[lang] || question.placeholder.en : '';
    if (placeholder) {
      message += `\n\n_${placeholder}_`;
    }
    if (question.optional) {
      message += '\n_(You can skip this by typing "skip")_';
    }
  }

  return message;
}

/**
 * Format 3-tier recommendation offers for WhatsApp.
 * @param {Object} offers - { basic, intermediate, premium } tier objects
 * @param {string} lang - Language code
 * @returns {string} Formatted message
 */
function formatOffers(offers, lang = 'en') {
  const labels = {
    en: {
      header: '🎯 *Your Personalized Recommendations*',
      basic: '🟢 *BASIC OPTION*',
      intermediate: '🔵 *INTERMEDIATE OPTIONS*',
      premium: '⭐ *PREMIUM RECOMMENDATION*',
      price: 'Price',
      score: 'Match',
      bundle: '🎁 Bundle includes',
      discount: 'Bundle discount',
      contact: 'To order, reply with *ORDER [product name]* or visit our website.',
    },
    pt: {
      header: '🎯 *Suas Recomendações Personalizadas*',
      basic: '🟢 *OPÇÃO BÁSICA*',
      intermediate: '🔵 *OPÇÕES INTERMEDIÁRIAS*',
      premium: '⭐ *RECOMENDAÇÃO PREMIUM*',
      price: 'Preço',
      score: 'Compatibilidade',
      bundle: '🎁 Pacote inclui',
      discount: 'Desconto do pacote',
      contact: 'Para pedir, responda com *PEDIR [nome do produto]* ou visite nosso site.',
    },
  };

  const t = labels[lang] || labels.en;
  let message = `${t.header}\n\n`;

  // Basic tier
  if (offers.basic && offers.basic.length > 0) {
    message += `${t.basic}\n`;
    offers.basic.forEach((p) => {
      message += `• *${p.name}* - $${p.price}\n`;
      if (p.description) message += `  ${p.description}\n`;
    });
    message += '\n';
  }

  // Intermediate tier
  if (offers.intermediate && offers.intermediate.length > 0) {
    message += `${t.intermediate}\n`;
    offers.intermediate.forEach((p) => {
      message += `• *${p.name}* - $${p.price} (${t.score}: ${p.score}%)\n`;
      if (p.reason) message += `  _${p.reason}_\n`;
    });
    message += '\n';
  }

  // Premium tier
  if (offers.premium) {
    message += `${t.premium}\n`;
    message += `🏆 *${offers.premium.product.name}* - $${offers.premium.product.price}\n`;
    if (offers.premium.product.description) {
      message += `${offers.premium.product.description}\n`;
    }
    if (offers.premium.bundle) {
      message += `\n${t.bundle}:\n`;
      offers.premium.bundle.items.forEach((item) => {
        message += `  + ${item}\n`;
      });
      message += `💰 ${t.discount}: ${offers.premium.bundle.discountPercent}% OFF\n`;
      message += `Total: $${offers.premium.bundle.totalPrice}\n`;
    }
    message += '\n';
  }

  message += `---\n${t.contact}`;
  return message;
}

/**
 * Format a simple text message with optional emoji prefix.
 */
function formatMessage(text, emoji = '') {
  return emoji ? `${emoji} ${text}` : text;
}

/**
 * Format welcome/greeting message.
 */
function formatWelcome(lang = 'en') {
  const messages = {
    en: `👋 *Welcome to Smart Product Advisor!*\n\nI'll help you find the perfect product by asking a few quick questions.\n\nThis will take about 2 minutes. Ready? Type *START* to begin!`,
    pt: `👋 *Bem-vindo ao Smart Product Advisor!*\n\nVou ajudá-lo a encontrar o produto perfeito fazendo algumas perguntas rápidas.\n\nIsso levará cerca de 2 minutos. Pronto? Digite *INICIAR* para começar!`,
  };
  return messages[lang] || messages.en;
}

/**
 * Format session timeout message.
 */
function formatTimeout(lang = 'en') {
  const messages = {
    en: `⏰ Your session has expired due to inactivity.\n\nType *START* to begin a new product search!`,
    pt: `⏰ Sua sessão expirou por inatividade.\n\nDigite *INICIAR* para começar uma nova busca de produto!`,
  };
  return messages[lang] || messages.en;
}

/**
 * Format error/invalid input message.
 */
function formatInvalidInput(optionCount, lang = 'en') {
  const messages = {
    en: `❌ Invalid choice. Please reply with a number between 1 and ${optionCount}.`,
    pt: `❌ Escolha inválida. Por favor, responda com um número entre 1 e ${optionCount}.`,
  };
  return messages[lang] || messages.en;
}

module.exports = {
  formatQuestion,
  formatOffers,
  formatMessage,
  formatWelcome,
  formatTimeout,
  formatInvalidInput,
};