File size: 7,129 Bytes
47b08cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// ===== 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 += `<div class="nova-msg nova-msg-user"><div class="nova-msg-content">${escapeHtml(query)}</div></div>`;

    // Hide suggestions after first message
    document.getElementById('novaSuggestions').style.display = 'none';

    // Add loading indicator
    const loadingId = 'nova-loading-' + Date.now();
    messages.innerHTML += `<div class="nova-msg nova-msg-bot" id="${loadingId}"><div class="nova-msg-content"><div class="nova-typing"><span></span><span></span><span></span></div></div></div>`;
    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:<br><br>
        <strong>Overall Performance:</strong><br>
        - Total Revenue: <strong>$30.2M</strong> (&#x25B2; 4.5% vs prior period)<br>
        - Total Marketing Spend: <strong>$21.3M</strong><br>
        - Overall ROAS: <strong>1.41x</strong><br><br>
        <strong>Key Highlights:</strong><br>
        - Paid Search leads with <strong>3.54x ROAS</strong> - highest efficiency<br>
        - Social Media showing strong momentum at <strong>2.60x ROAS</strong><br>
        - TV and Print are below break-even and need attention<br><br>
        <em>Recommendation:</em> 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 `<strong>Underperforming Channels:</strong><br><br>
        1. <strong>Print</strong> - ROAS: 0.08x (critically low)<br>
           &bull; Only generating $0.08 per dollar spent<br>
           &bull; Very low CTR indicates poor audience engagement<br><br>
        2. <strong>TV</strong> - ROAS: 0.28x (below break-even)<br>
           &bull; Highest spend ($14.5M) but diminishing returns<br>
           &bull; May be past saturation point<br><br>
        3. <strong>Radio</strong> - ROAS: 0.36x<br>
           &bull; Low conversion rate and limited attribution<br><br>
        <em>Action:</em> 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 `<strong>Top Growth Opportunities:</strong><br><br>
        1. <strong>Paid Search Scaling</strong> &#x1F680;<br>
           &bull; Currently at 3.54x ROAS with only 17.5% of budget<br>
           &bull; Estimated headroom: +25% spend increase profitable<br><br>
        2. <strong>Social Media Expansion</strong> &#x2197;&#xFE0F;<br>
           &bull; 2.60x ROAS with growing audience<br>
           &bull; Instagram and Facebook showing strong engagement<br><br>
        3. <strong>Budget Reallocation</strong> &#x1F4B0;<br>
           &bull; Moving $2M from TV/Print to Digital could yield est. +$3.5M revenue<br><br>
        Use the <strong>Simulation panel</strong> 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:<br><br>
        <strong>If you shift 20% of TV budget to Paid Search:</strong><br>
        - TV spend: $14.5M &#x2192; $11.6M (-$2.9M)<br>
        - Paid Search spend: $7.5M &#x2192; $10.4M (+$2.9M)<br>
        - Estimated revenue impact: <strong>+$2.1M</strong><br>
        - New overall ROAS: <strong>1.48x</strong> (from 1.41x)<br><br>
        For detailed simulation with more controls, use the <strong>"Simulate"</strong> button on any alert card or open the simulation panel.`;
    }

    if (q.includes('explain') || q.includes('why')) {
        return `Let me analyze that for you:<br><br>
        The most likely drivers behind this pattern are:<br><br>
        1. <strong>Saturation Effects</strong> - High-spend channels like TV may be past their efficiency peak, where additional spend yields diminishing returns<br><br>
        2. <strong>Audience Overlap</strong> - Multiple channels may be reaching the same audience segments, creating redundancy<br><br>
        3. <strong>Seasonal Patterns</strong> - Performance naturally fluctuates with consumer behavior cycles<br><br>
        <em>I recommend running the Diagnostic Analysis for deeper statistical decomposition of these effects.</em>`;
    }

    // Default response
    return `That's a great question! Based on the current marketing data:<br><br>
    I'd suggest looking at the <strong>channel-level performance metrics</strong> for specific insights. The key areas to focus on are:<br><br>
    - Channel ROAS comparison for efficiency<br>
    - Spend allocation vs. returns<br>
    - Trend analysis for momentum indicators<br><br>
    Would you like me to dive deeper into any of these areas? You can also try:<br>
    - "Which channels are underperforming?"<br>
    - "What are the top opportunities?"<br>
    - "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);