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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
// Global Variables
let mediaRecorder = null;
let audioChunks = [];
let isRecording = false;
let recordingStartTime = null;
let timerInterval = null;
let currentAudio = null;
let responseLanguage = 'en'; // 'en' for English only, 'si-en' for Sinhala+English

// DOM Elements - Voice Chat
const micBtn = document.getElementById('micBtn');
const statusIndicator = document.getElementById('statusIndicator');
const statusDot = statusIndicator.querySelector('.status-dot');
const statusText = statusIndicator.querySelector('.status-text');
const recordingTimer = document.getElementById('recordingTimer');
const timerText = recordingTimer.querySelector('.timer-text');
const visualizer = document.getElementById('visualizer');
const userText = document.getElementById('userText');
const botText = document.getElementById('botText');
const speakerBtn = document.getElementById('speakerBtn');
const pauseBtn = document.getElementById('pauseBtn');
const loadingOverlay = document.getElementById('loadingOverlay');
const loadingText = document.getElementById('loadingText');
const chatContainer = document.getElementById('chatContainer');
const resetBtn = document.getElementById('resetBtn');

// DOM Elements - Sections
const voiceChatSection = document.getElementById('voiceChatSection');

// Initialize
document.addEventListener('DOMContentLoaded', () => {
    checkBrowserSupport();
    setupEventListeners();
});

// Check browser support for audio recording
function checkBrowserSupport() {
    if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
        showError('Your browser does not support audio recording. Please use a modern browser like Chrome or Firefox.');
        micBtn.disabled = true;
    }
}

// Setup Event Listeners
function setupEventListeners() {
    micBtn.addEventListener('click', toggleRecording);
    speakerBtn.addEventListener('click', playResponse);
    
    // Pause button
    if (pauseBtn) {
        pauseBtn.addEventListener('click', pauseAudio);
    }

    // Reset button - also clears history
    if (resetBtn) {
        resetBtn.addEventListener('click', resetRecording);
    }
}

// Toggle Recording
async function toggleRecording() {
    if (isRecording) {
        stopRecording();
    } else {
        await startRecording();
    }
}

// Start Recording
async function startRecording() {
    try {
        const stream = await navigator.mediaDevices.getUserMedia({ 
            audio: {
                sampleRate: 16000,
                channelCount: 1,
                echoCancellation: true,
                noiseSuppression: true
            } 
        });

        // Determine the best supported MIME type
        let mimeType = 'audio/webm';
        if (MediaRecorder.isTypeSupported('audio/webm;codecs=opus')) {
            mimeType = 'audio/webm;codecs=opus';
        } else if (MediaRecorder.isTypeSupported('audio/webm')) {
            mimeType = 'audio/webm';
        } else if (MediaRecorder.isTypeSupported('audio/mp4')) {
            mimeType = 'audio/mp4';
        } else if (MediaRecorder.isTypeSupported('audio/ogg')) {
            mimeType = 'audio/ogg';
        }

        mediaRecorder = new MediaRecorder(stream, { mimeType });
        audioChunks = [];

        mediaRecorder.ondataavailable = (event) => {
            if (event.data.size > 0) {
                audioChunks.push(event.data);
            }
        };

        mediaRecorder.onstop = async () => {
            const audioBlob = new Blob(audioChunks, { type: mimeType });
            stream.getTracks().forEach(track => track.stop());
            await processAudio(audioBlob);
        };

        mediaRecorder.start(100); // Collect data every 100ms
        isRecording = true;
        recordingStartTime = Date.now();

        // Update UI
        updateUIForRecording(true);
        startTimer();

    } catch (error) {
        console.error('Error starting recording:', error);
        showError('Could not access microphone. Please allow microphone permission.');
    }
}

// Stop Recording
function stopRecording() {
    if (mediaRecorder && mediaRecorder.state !== 'inactive') {
        mediaRecorder.stop();
        isRecording = false;
        stopTimer();
        updateUIForRecording(false);
    }
}

// Update UI for Recording State
function updateUIForRecording(recording) {
    if (recording) {
        micBtn.classList.add('recording');
        statusDot.classList.add('recording');
        statusText.textContent = 'Recording...';
        recordingTimer.classList.add('active');
        visualizer.classList.add('active');
    } else {
        micBtn.classList.remove('recording');
        statusDot.classList.remove('recording');
        statusText.textContent = 'Processing...';
        recordingTimer.classList.remove('active');
        visualizer.classList.remove('active');
    }
}

// Timer Functions
function startTimer() {
    timerInterval = setInterval(() => {
        const elapsed = Date.now() - recordingStartTime;
        const minutes = Math.floor(elapsed / 60000);
        const seconds = Math.floor((elapsed % 60000) / 1000);
        timerText.textContent = `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
    }, 100);
}

function stopTimer() {
    if (timerInterval) {
        clearInterval(timerInterval);
        timerInterval = null;
    }
    timerText.textContent = '00:00';
}

// Process Audio - Send to Backend
async function processAudio(audioBlob) {
    showLoading('Converting speech to text...');

    try {
        // Convert to WAV format for better compatibility
        const wavBlob = await convertToWav(audioBlob);
        
        // Create form data
        const formData = new FormData();
        formData.append('audio', wavBlob, 'recording.wav');

        // Send to speech-to-text endpoint
        const sttResponse = await fetch('/api/speech-to-text', {
            method: 'POST',
            body: formData
        });

        if (!sttResponse.ok) {
            const error = await sttResponse.json();
            throw new Error(error.detail || 'Speech recognition failed');
        }

        const sttResult = await sttResponse.json();
        const transcribedText = sttResult.text;

        // Show original transcription temporarily
        displayUserText(transcribedText + ' (translating...)');

        // Step 2: Translate to English
        showLoading('Translating to English...');
        let englishText = transcribedText;
        let translationSuccess = false;
        
        try {
            const translateRes = await fetch('/api/translate-to-english', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ question: transcribedText })
            });
            
            if (translateRes.ok) {
                const translateData = await translateRes.json();
                if (translateData.translated && translateData.english_question) {
                    englishText = translateData.english_question;
                    translationSuccess = true;
                } else if (translateData.english_question && translateData.english_question !== transcribedText) {
                    // Even if translated flag is false, check if we got different text
                    englishText = translateData.english_question;
                    translationSuccess = true;
                }
            }
        } catch (translateError) {
            console.error('Translation error:', translateError);
        }

        // Display both original and English if translation succeeded, otherwise just show original
        if (translationSuccess && englishText !== transcribedText) {
            displayUserTextWithOriginal(transcribedText, englishText);
        } else {
            displayUserText(transcribedText + ' (translation failed - using original)');
        }

        // Step 3: Use RAG first, fallback to Gemini API
        showLoading('Searching knowledge base...');
        const ragResponse = await fetch('/api/rag/ask', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ 
                question: englishText,
                response_lang: responseLanguage  // 'en' or 'si-en'
            })
        });

        if (!ragResponse.ok) {
            const error = await ragResponse.json();
            throw new Error(error.detail || 'Query failed');
        }

        const ragResult = await ragResponse.json();
        const botResponse = ragResult.answer;
        const source = ragResult.source; // 'rag', 'gemini', or 'none'

        // Display bot response with source indicator
        displayBotTextWithSource(botResponse, source);

        // Enable speaker button
        speakerBtn.disabled = false;

        // Update status
        updateStatus('ready', 'Ready');

    } catch (error) {
        console.error('Processing error:', error);
        showError(error.message);
        updateStatus('ready', 'Ready');
    } finally {
        hideLoading();
    }
}

// Convert audio blob to WAV format
async function convertToWav(audioBlob) {
    return new Promise((resolve, reject) => {
        const audioContext = new (window.AudioContext || window.webkitAudioContext)();
        const reader = new FileReader();

        reader.onload = async () => {
            try {
                const arrayBuffer = reader.result;
                const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
                
                // Resample to 16kHz for Whisper model
                const targetSampleRate = 16000;
                const offlineContext = new OfflineAudioContext(
                    1, // mono
                    audioBuffer.duration * targetSampleRate,
                    targetSampleRate
                );

                const source = offlineContext.createBufferSource();
                source.buffer = audioBuffer;
                source.connect(offlineContext.destination);
                source.start(0);

                const renderedBuffer = await offlineContext.startRendering();
                const wavBlob = audioBufferToWav(renderedBuffer);
                resolve(wavBlob);
            } catch (error) {
                // If conversion fails, return original blob
                console.warn('WAV conversion failed, using original format:', error);
                resolve(audioBlob);
            }
        };

        reader.onerror = () => reject(reader.error);
        reader.readAsArrayBuffer(audioBlob);
    });
}

// Convert AudioBuffer to WAV Blob
function audioBufferToWav(buffer) {
    const numChannels = buffer.numberOfChannels;
    const sampleRate = buffer.sampleRate;
    const format = 1; // PCM
    const bitDepth = 16;

    const bytesPerSample = bitDepth / 8;
    const blockAlign = numChannels * bytesPerSample;

    const dataLength = buffer.length * blockAlign;
    const bufferLength = 44 + dataLength;

    const arrayBuffer = new ArrayBuffer(bufferLength);
    const view = new DataView(arrayBuffer);

    // WAV header
    writeString(view, 0, 'RIFF');
    view.setUint32(4, 36 + dataLength, true);
    writeString(view, 8, 'WAVE');
    writeString(view, 12, 'fmt ');
    view.setUint32(16, 16, true);
    view.setUint16(20, format, true);
    view.setUint16(22, numChannels, true);
    view.setUint32(24, sampleRate, true);
    view.setUint32(28, sampleRate * blockAlign, true);
    view.setUint16(32, blockAlign, true);
    view.setUint16(34, bitDepth, true);
    writeString(view, 36, 'data');
    view.setUint32(40, dataLength, true);

    // Write audio data
    const channelData = buffer.getChannelData(0);
    let offset = 44;
    for (let i = 0; i < channelData.length; i++) {
        const sample = Math.max(-1, Math.min(1, channelData[i]));
        view.setInt16(offset, sample < 0 ? sample * 0x8000 : sample * 0x7FFF, true);
        offset += 2;
    }

    return new Blob([arrayBuffer], { type: 'audio/wav' });
}

function writeString(view, offset, string) {
    for (let i = 0; i < string.length; i++) {
        view.setUint8(offset + i, string.charCodeAt(i));
    }
}

// Display Functions
function displayUserText(text) {
    userText.innerHTML = `<p>${escapeHtml(text)}</p>`;
}

function displayUserTextWithOriginal(originalText, englishText) {
    userText.innerHTML = `
        <p>${escapeHtml(originalText)}</p>
    `;
}

function displayBotText(text) {
    // Convert markdown-like formatting to HTML
    const formattedText = formatText(text);
    botText.innerHTML = formattedText;
}

function displayBotTextWithSource(text, source) {
    // Convert markdown-like formatting to HTML with source badge
    const formattedText = formatText(text);
    let sourceLabel = '';
    if (source === 'rag') {
        sourceLabel = '<span class="source-badge source-rag"><i class="fas fa-database"></i> From Documents</span>';
    } else if (source === 'gemini') {
        sourceLabel = '<span class="source-badge source-gemini"><i class="fas fa-brain"></i> From AI</span>';
    }
    botText.innerHTML = sourceLabel + formattedText;
}

function formatText(text) {
    // Basic formatting
    let formatted = escapeHtml(text);
    
    // Convert line breaks
    formatted = formatted.replace(/\n/g, '<br>');
    
    // Convert **bold** to <strong>
    formatted = formatted.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
    
    // Convert *italic* to <em>
    formatted = formatted.replace(/\*(.*?)\*/g, '<em>$1</em>');
    
    return `<p>${formatted}</p>`;
}

function escapeHtml(text) {
    const div = document.createElement('div');
    div.textContent = text;
    return div.innerHTML;
}

// Play Response using TTS
async function playResponse() {
    const text = botText.textContent || botText.innerText;
    
    if (!text || text.includes('will appear here')) {
        return;
    }

    // If paused, resume
    if (currentAudio && currentAudio.paused) {
        currentAudio.play();
        speakerBtn.classList.add('playing');
        pauseBtn.classList.remove('paused');
        pauseBtn.querySelector('i').className = 'fas fa-pause';
        return;
    }

    // Stop current audio if playing
    if (currentAudio) {
        currentAudio.pause();
        currentAudio = null;
        speakerBtn.classList.remove('playing');
    }

    speakerBtn.classList.add('playing');
    speakerBtn.querySelector('i').className = 'fas fa-spinner fa-spin';

    try {
        const ttsLang = responseLanguage === 'en' ? 'en' : 'si';

        const response = await fetch('/api/text-to-speech', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ 
                text: text,
                lang: ttsLang
            })
        });

        if (!response.ok) {
            throw new Error('Text-to-speech failed');
        }

        const audioBlob = await response.blob();
        const audioUrl = URL.createObjectURL(audioBlob);
        
        currentAudio = new Audio(audioUrl);
        
        currentAudio.onended = () => {
            speakerBtn.classList.remove('playing');
            speakerBtn.querySelector('i').className = 'fas fa-volume-up';
            pauseBtn.classList.remove('paused');
            pauseBtn.querySelector('i').className = 'fas fa-pause';
            URL.revokeObjectURL(audioUrl);
            currentAudio = null;
        };

        currentAudio.onerror = () => {
            speakerBtn.classList.remove('playing');
            speakerBtn.querySelector('i').className = 'fas fa-volume-up';
            showError('Failed to play audio');
        };

        await currentAudio.play();
        speakerBtn.querySelector('i').className = 'fas fa-volume-up';

    } catch (error) {
        console.error('TTS error:', error);
        speakerBtn.classList.remove('playing');
        speakerBtn.querySelector('i').className = 'fas fa-volume-up';
        showError('Text-to-speech failed');
    }
}

// Pause Audio Playback
function pauseAudio() {
    if (currentAudio && !currentAudio.paused) {
        currentAudio.pause();
        speakerBtn.classList.remove('playing');
        pauseBtn.classList.add('paused');
        pauseBtn.querySelector('i').className = 'fas fa-play';
    } else if (currentAudio && currentAudio.paused) {
        currentAudio.play();
        speakerBtn.classList.add('playing');
        pauseBtn.classList.remove('paused');
        pauseBtn.querySelector('i').className = 'fas fa-pause';
    }
}

// Reset Recording / Stop current action
function resetRecording() {
    if (isRecording) {
        stopRecording();
    }
    if (currentAudio) {
        currentAudio.pause();
        currentAudio = null;
        speakerBtn.classList.remove('playing');
    }
    updateStatus('ready', 'Ready');
    clearHistory();
}

// Clear Conversation History
async function clearHistory() {
    try {
        const response = await fetch('/api/clear-history', {
            method: 'POST'
        });

        if (response.ok) {
            // Reset UI
            userText.innerHTML = '<p class="placeholder">Your transcribed message will appear here...</p>';
            botText.innerHTML = '<p class="placeholder">Bot response will appear here...</p>';
            speakerBtn.disabled = true;
            
            // Show confirmation
            showSuccess('Conversation history cleared');
        }
    } catch (error) {
        console.error('Error clearing history:', error);
        showError('Failed to clear history');
    }
}

// Loading Functions
function showLoading(message = 'Processing...') {
    loadingText.textContent = message;
    loadingOverlay.classList.add('active');
}

function hideLoading() {
    loadingOverlay.classList.remove('active');
}

// Status Update
function updateStatus(state, text) {
    statusDot.className = 'status-dot';
    if (state !== 'ready') {
        statusDot.classList.add(state);
    }
    statusText.textContent = text;
}

// Notification Functions
function showError(message) {
    // Create toast notification
    showToast(message, 'error');
    // Clear user and bot input fields after 2 seconds
    setTimeout(() => {
        if (userText) {
            userText.innerHTML = '<p class="placeholder">Your transcribed message will appear here...</p>';
        }
        if (botText) {
            botText.innerHTML = '<p class="placeholder">Bot response will appear here...</p>';
        }
    }, 2000);
}

function showSuccess(message) {
    showToast(message, 'success');
}

function showToast(message, type = 'info') {
    // Remove existing toasts
    const existingToasts = document.querySelectorAll('.toast');
    existingToasts.forEach(t => t.remove());

    // Create toast element
    const toast = document.createElement('div');
    toast.className = `toast toast-${type}`;
    toast.innerHTML = `
        <i class="fas ${type === 'error' ? 'fa-exclamation-circle' : 'fa-check-circle'}"></i>
        <span>${message}</span>
    `;

    // Add styles
    toast.style.cssText = `
        position: fixed;
        bottom: 20px;
        left: 50%;
        transform: translateX(-50%);
        padding: 12px 24px;
        background: ${type === 'error' ? '#ef4444' : '#22c55e'};
        color: white;
        border-radius: 8px;
        display: flex;
        align-items: center;
        gap: 10px;
        z-index: 2000;
        box-shadow: 0 4px 20px rgba(0,0,0,0.3);
        animation: slideUp 0.3s ease;
    `;

    // Add animation keyframes if not exists
    if (!document.getElementById('toast-styles')) {
        const style = document.createElement('style');
        style.id = 'toast-styles';
        style.textContent = `
            @keyframes slideUp {
                from { transform: translateX(-50%) translateY(100%); opacity: 0; }
                to { transform: translateX(-50%) translateY(0); opacity: 1; }
            }
        `;
        document.head.appendChild(style);
    }

    document.body.appendChild(toast);

    // Remove after 4 seconds
    setTimeout(() => {
        toast.style.opacity = '0';
        toast.style.transition = 'opacity 0.3s ease';
        setTimeout(() => toast.remove(), 300);
    }, 4000);
}