// ===== NOVA — Next-gen Optimization & Value Advisor ===== function toggleNOVA() { const drawer = document.getElementById('novaDrawer'); const overlay = document.getElementById('novaOverlay'); const fab = document.getElementById('novaFab'); const isOpen = drawer.classList.contains('open'); drawer.classList.toggle('open'); overlay.classList.toggle('open'); if (fab) fab.style.display = isOpen ? '' : 'none'; if (!isOpen) { document.getElementById('novaInput').focus(); } } function sendNOVAFromInput() { const input = document.getElementById('novaInput'); const query = input.value.trim(); if (!query) return; input.value = ''; sendNOVA(query); } function sendNOVA(query) { // Ensure drawer is open const drawer = document.getElementById('novaDrawer'); if (!drawer.classList.contains('open')) toggleNOVA(); const messages = document.getElementById('novaMessages'); // Add user message messages.innerHTML += `
${escapeHtml(query)}
`; // Hide suggestions after first message document.getElementById('novaSuggestions').style.display = 'none'; // Add loading indicator const loadingId = 'nova-loading-' + Date.now(); messages.innerHTML += `
`; messages.scrollTop = messages.scrollHeight; // Generate contextual response setTimeout(() => { const response = generateNOVAResponse(query); const loadingEl = document.getElementById(loadingId); if (loadingEl) { loadingEl.querySelector('.nova-msg-content').innerHTML = response; } messages.scrollTop = messages.scrollHeight; }, 800 + Math.random() * 1200); } function generateNOVAResponse(query) { const q = query.toLowerCase(); if (q.includes('summarize') || q.includes('summary') || q.includes('performance')) { return `Based on the current data:

Overall Performance:
- Total Revenue: $30.2M (▲ 4.5% vs prior period)
- Total Marketing Spend: $21.3M
- Overall ROAS: 1.41x

Key Highlights:
- Paid Search leads with 3.54x ROAS - highest efficiency
- Social Media showing strong momentum at 2.60x ROAS
- TV and Print are below break-even and need attention

Recommendation: Consider shifting 15-20% of TV budget to Paid Search and Social Media for improved returns.`; } if (q.includes('underperform') || q.includes('low') || q.includes('drop') || q.includes('decline')) { return `Underperforming Channels:

1. Print - ROAS: 0.08x (critically low)
• Only generating $0.08 per dollar spent
• Very low CTR indicates poor audience engagement

2. TV - ROAS: 0.28x (below break-even)
• Highest spend ($14.5M) but diminishing returns
• May be past saturation point

3. Radio - ROAS: 0.36x
• Low conversion rate and limited attribution

Action: These channels may benefit from budget reduction and reallocation to digital channels with proven higher returns.`; } if (q.includes('opportunit') || q.includes('growth') || q.includes('scale')) { return `Top Growth Opportunities:

1. Paid Search Scaling 🚀
• Currently at 3.54x ROAS with only 17.5% of budget
• Estimated headroom: +25% spend increase profitable

2. Social Media Expansion ↗️
• 2.60x ROAS with growing audience
• Instagram and Facebook showing strong engagement

3. Budget Reallocation 💰
• Moving $2M from TV/Print to Digital could yield est. +$3.5M revenue

Use the Simulation panel to model these scenarios with precise projections.`; } if (q.includes('simulat') || q.includes('what if') || q.includes('what-if') || q.includes('budget')) { return `I can help you simulate budget scenarios. Here's a quick analysis:

If you shift 20% of TV budget to Paid Search:
- TV spend: $14.5M → $11.6M (-$2.9M)
- Paid Search spend: $7.5M → $10.4M (+$2.9M)
- Estimated revenue impact: +$2.1M
- New overall ROAS: 1.48x (from 1.41x)

For detailed simulation with more controls, use the "Simulate" button on any alert card or open the simulation panel.`; } if (q.includes('explain') || q.includes('why')) { return `Let me analyze that for you:

The most likely drivers behind this pattern are:

1. Saturation Effects - High-spend channels like TV may be past their efficiency peak, where additional spend yields diminishing returns

2. Audience Overlap - Multiple channels may be reaching the same audience segments, creating redundancy

3. Seasonal Patterns - Performance naturally fluctuates with consumer behavior cycles

I recommend running the Diagnostic Analysis for deeper statistical decomposition of these effects.`; } // Default response return `That's a great question! Based on the current marketing data:

I'd suggest looking at the channel-level performance metrics for specific insights. The key areas to focus on are:

- Channel ROAS comparison for efficiency
- Spend allocation vs. returns
- Trend analysis for momentum indicators

Would you like me to dive deeper into any of these areas? You can also try:
- "Which channels are underperforming?"
- "What are the top opportunities?"
- "Simulate shifting budget from TV to Digital"`; } function escapeHtml(text) { const div = document.createElement('div'); div.textContent = text; return div.innerHTML; } // Load alert count for bell badge function loadAlertBellCount() { fetch('/alerts/api/alerts/count') .then(r => r.json()) .then(data => { const badge = document.getElementById('alertBellCount'); if (badge) { badge.textContent = data.count; badge.style.display = data.count > 0 ? '' : 'none'; } }) .catch(() => {}); } document.addEventListener('DOMContentLoaded', loadAlertBellCount);