// commands/voiceReport.js import { logger } from '../utils/logger.js'; import { collectVoiceReport } from '../services/voiceReport/voiceReportService.js'; import { renderVoiceReportMarkdown } from '../services/renderers/voiceReportRenderer.js'; function pickDateArg(args) { for (let i = 1; i < args.length; i++) { const a = String(args[i] || '').trim(); if (!a || a.startsWith('--')) continue; if (/^\d{2,4}$/.test(a) && i === 1) continue; return a; } const flag = args.find((a) => String(a).startsWith('--date=')); if (flag) return flag.split('=').slice(1).join('='); const idx = args.indexOf('--date'); if (idx >= 0 && args[idx + 1]) return args[idx + 1]; return null; } function hasDetailFlag(args, query) { return args.some((a) => ['detail', 'detailed', '--detail', '--detailed'].includes(String(a).toLowerCase())) || query.detail === 'true' || query.detailed === 'true'; } export async function handleVoiceReport(bot, trigger) { logger('voicereport', 'Handler entered', 'debug'); const query = trigger.query || {}; const args = trigger.args || []; let storeNum = args[0]?.trim() || query.storeNum || query.store || query.s; const dateArg = pickDateArg(args) || query.date || null; const detail = hasDetailFlag(args, query); if (!storeNum || !/^\d{2,4}$/.test(storeNum)) { await bot.say( 'markdown', '**Voice report usage**\n\n' + '`/voicereport ` — yesterday 9am–9pm local (default)\n' + '`/voicereport today` — today 9am → 5 min ago\n' + '`/voicereport 2026-07-22` — specific day\n' + '`/voicereport --detail` — include per-call table\n\n' + 'HTTP: `?storeNum=782&date=yesterday&detail=true`', ); return; } await bot.say( 'markdown', `**Voice report** — generating for store **${storeNum}** ` + `(${dateArg || 'yesterday'})… This may take 1–3 minutes while Webex builds the Media Quality report.`, ); try { const report = await collectVoiceReport(storeNum, { dateArg }); const md = renderVoiceReportMarkdown(report, { detail }); await bot.say('markdown', md); } catch (err) { logger('voicereport', `Failed for store ${storeNum}: ${err.message}`, 'error'); await bot.say('markdown', `❌ Voice report failed for store ${storeNum}: ${err.message}`); } }