File size: 15,706 Bytes
ed0b977
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
document.addEventListener("DOMContentLoaded", () => {
    // --- Element Selectors ---
    const userInput = document.getElementById("user-input");
    const sendButton = document.getElementById("send-button");
    const chatHistory = document.getElementById("chat-history");
    const menuItems = document.querySelectorAll(".menu-item");
    const convoRecord = document.getElementById("convo-record");

    // Modal Elements
    const patientModal = document.getElementById("patient-modal");
    const patientForm = document.getElementById("patient-form");
    const patientIdInput = document.getElementById("patient-id-input");

    // Track session for backend context
    let currentSessionId = window.activeSessionId || null;
    let similarSearchActive = false; // --- NEW: Toggle state for similar search

    // --- NEW: Function to control chat input state ---
    function setChatInputDisabled(disabled) {
        if (userInput) {
            userInput.disabled = disabled;
            userInput.placeholder = disabled ? "Please start a patient session first..." : "Ask Swift Query AI...";
        }
        if (sendButton) {
            sendButton.disabled = disabled;
            sendButton.classList.toggle('bg-gray-400', disabled);
            sendButton.classList.toggle('bg-primary', !disabled);
            sendButton.classList.toggle('hover:bg-orange-600', !disabled);
        }
    }

    // --- Feature Handlers (MODIFIED) ---
    const featureHandlers = {
        "chat-feature": () => {
            if (similarSearchActive) {
                // If similar search was active, turn it off
                toggleSimilarSearch(document.getElementById('similar-cases-feature'));
            }
            if (userInput) userInput.focus();
        },
        "similar-cases-feature": (event) => {
            if (!window.activePatientId) {
                appendMessage("Please start a patient session to find similar cases.", "ai", new Date());
                return;
            }
            // NEW: Toggle the similar search mode
            toggleSimilarSearch(event.currentTarget);
        },
        "patient-search-feature": () => {
            if (patientModal) {
                patientModal.classList.remove("hidden");
                if (patientIdInput) setTimeout(() => patientIdInput.focus(), 100);
            }
        },
        "clear-chat-feature": () => {
            const greeting = chatHistory.firstChild;
            chatHistory.innerHTML = '';
            if(greeting) chatHistory.appendChild(greeting);
            appendMessage("Local chat view cleared.", "ai", new Date());
        },
        "history-feature": () => {
            loadHistoryList();
        }
    };

    // --- NEW: Toggle function for Similar Search ---
    function toggleSimilarSearch(buttonElement) {
        similarSearchActive = !similarSearchActive;
        if (!userInput || !buttonElement) return;

        if (similarSearchActive) {
            userInput.value = "/similar ";
            userInput.focus();
            buttonElement.classList.add("active"); // Add visual indicator
            appendMessage("**Similar Case Search Activated.**\nPlease describe the symptoms or conditions to find matching cases.", "ai", new Date());
        } else {
            if (userInput.value.startsWith("/similar ")) {
                userInput.value = "";
            }
            buttonElement.classList.remove("active");
            appendMessage("**Similar Case Search Deactivated.**", "ai", new Date());
        }
    }

    // --- Event Listeners ---
    menuItems.forEach(item => {
        if (item.tagName === 'A' && !item.href.includes('logout')) {
            item.addEventListener("click", (e) => {
                if (featureHandlers[item.id]) {
                    e.preventDefault();
                    // Don't deactivate similar search button when it's clicked
                    if (item.id !== 'similar-cases-feature') {
                        const activeItem = document.querySelector(".menu-item.active");
                        if (activeItem) activeItem.classList.remove("active");
                        item.classList.add("active");
                    }
                    featureHandlers[item.id](e);
                }
            });
        }
    });

    if (patientForm) {
        patientForm.addEventListener("submit", (e) => {
            e.preventDefault();
            const pid = patientIdInput.value.trim();
            if (pid) {
                if (patientModal) patientModal.classList.add("hidden");
                startPatientSession(pid);
            }
        });
    }

    // --- Recording Logic ---
    let mediaRecorder = null;
    let audioChunks = [];

    window.startRecording = async function () {
        if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) return;
        try {
            const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
            mediaRecorder = new MediaRecorder(stream);
            audioChunks = [];
            mediaRecorder.ondataavailable = (event) => {
                if (event.data.size > 0) audioChunks.push(event.data);
            };
            mediaRecorder.onstop = async () => {
                const audioBlob = new Blob(audioChunks, { type: 'audio/mp3' });
                appendMessage("Processing recording... ⏳", "ai", new Date());
                const formData = new FormData();
                formData.append("audio", audioBlob, "recording.mp3");
                formData.append("patient_id", window.activePatientId);

                try {
                    const response = await fetch("/doctor/upload_recording", { method: "POST", body: formData });
                    const result = await response.json();
                    if (result.success) {
                        appendMessage(`### 🎙️ Recording Saved\n\n**Processed Note:**\n${result.data.processed_note}`, "ai", new Date());
                    } else {
                        appendMessage(`❌ Recording failed: ${result.error}`, "ai", new Date());
                    }
                } catch (err) {
                    console.error(err);
                    appendMessage("❌ Error uploading recording.", "ai", new Date());
                }
                stream.getTracks().forEach(track => track.stop());
            };
            mediaRecorder.start();
            appendMessage("🎙️ **Recording Started...**", "ai", new Date());
            if (convoRecord) convoRecord.checked = true;
        } catch (err) {
            console.error("Microphone access denied:", err);
            if (convoRecord) convoRecord.checked = false;
            alert("Microphone access is required to record.");
        }
    };

    window.stopRecording = function () {
        if (mediaRecorder && mediaRecorder.state !== "inactive") {
            mediaRecorder.stop();
            appendMessage("⏹️ **Recording Stopped.** Processing...", "ai", new Date());
            if (convoRecord) convoRecord.checked = false;
        }
    };

    if (convoRecord) {
        convoRecord.addEventListener("change", async () => {
            if (!window.activePatientId) {
                convoRecord.checked = false;
                alert("Please start a patient session before starting a recording.");
                return;
            }
            if (convoRecord.checked) await window.startRecording();
            else window.stopRecording();
        });
    }

    // --- Core Chat Functions ---
    if (sendButton) sendButton.addEventListener("click", sendMessage);
    if (userInput) {
        userInput.addEventListener("keypress", (e) => {
            if (e.key === "Enter" && !e.shiftKey) {
                e.preventDefault();
                sendMessage();
            }
        });
        userInput.addEventListener("input", function () {
            this.style.height = "50px";
            this.style.height = `${Math.min(this.scrollHeight, 200)}px`;
        });
    }

    function startPatientSession(patientId) {
        setChatInputDisabled(true);
        if (chatHistory) chatHistory.innerHTML = '';
        appendMessage(`Starting session for Patient ID: ${patientId}... This may take a moment.`, "ai", new Date());

        fetch(`/doctor/start_session/${patientId}`, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
        })
        .then(res => res.json())
        .then(data => {
            if (data.success) {
                appendMessage(`Session for patient ${data.patient_id} is ready. Reloading dashboard...`, "ai", new Date());
                setTimeout(() => window.location.reload(), 500);
            } else {
                console.error("Error starting session:", data.error);
                appendMessage(`Failed to start session: ${data.error}`, "ai", new Date());
                setChatInputDisabled(false);
            }
        })
        .catch(err => {
            console.error("Network error starting session:", err);
            appendMessage("A network error occurred while starting the session.", "ai", new Date());
            setChatInputDisabled(false);
        });
    }

    function loadHistoryList() {
        chatHistory.innerHTML = '';
        appendMessage("Loading history sessions...", "ai", new Date());

        fetch("/api/history")
            .then(res => res.json())
            .then(data => {
                chatHistory.innerHTML = '';
                if (data.history && data.history.length > 0) {
                    appendMessage("### Select a Past Session to Resume:", "ai", new Date());
                    const list = document.createElement("div");
                    list.className = "flex flex-col gap-2 mt-2";
                    data.history.forEach(item => {
                        const btn = document.createElement("button");
                        btn.className = "bg-white p-3 rounded-lg shadow-sm hover:bg-orange-50 text-left border border-gray-200 transition-colors w-full group";
                        btn.innerHTML = `<div class='font-bold text-primary flex justify-between'><span>${item.title || 'Untitled'}</span><i class="fas fa-chevron-right text-gray-300 group-hover:text-primary transition-colors"></i></div><div class='text-xs text-gray-500 mt-1'><i class="far fa-clock mr-1"></i> ${item.display_time || 'Unknown date'}</div>`;
                        btn.onclick = () => window.loadHistoryItem(item.type || 'chat', item.session_id);
                        list.appendChild(btn);
                    });
                    const container = document.createElement("div");
                    container.className = "w-full max-w-lg mb-4";
                    container.appendChild(list);
                    chatHistory.appendChild(container);
                    chatHistory.scrollTop = chatHistory.scrollHeight;
                } else {
                    appendMessage("No past sessions found.", "ai", new Date());
                }
            })
            .catch(err => {
                console.error(err);
                appendMessage("Failed to load history.", "ai", new Date());
            });
    }

    window.loadHistoryItem = function (type, id) {
        chatHistory.innerHTML = '';
        appendMessage("Restoring...", "ai", new Date());
        currentSessionId = (type === 'chat') ? id : null;

        fetch(`/api/history/${type}/${id}`)
            .then(res => res.json())
            .then(data => {
                chatHistory.innerHTML = '';
                if (data.messages && data.messages.length > 0) {
                    data.messages.forEach(msg => appendMessage(msg.content, msg.sender, msg.timestamp || new Date()));
                    if (type === 'chat') appendMessage("--- Session Restored ---", "ai", new Date());
                } else {
                    appendMessage("Item is empty.", "ai", new Date());
                }
            })
            .catch(err => {
                console.error(err);
                appendMessage("Failed to restore item.", "ai", new Date());
            });
    };

    function sendMessage() {
        if (!userInput || userInput.disabled) return;
        const message = userInput.value.trim();
        if (message === "") return;

        appendMessage(message, "user", new Date());
        userInput.value = "";
        userInput.style.height = "50px";
        
        fetchAIResponse(message);

        // NEW: Deactivate similar search after sending the query
        if (similarSearchActive) {
            toggleSimilarSearch(document.getElementById('similar-cases-feature'));
        }
    }

    async function fetchAIResponse(message) {
        if (!window.activePatientId || !currentSessionId) {
            appendMessage("Cannot send message. No active patient session.", "ai", new Date());
            return;
        }

        try {
            const response = await fetch("/api/chat", {
                method: "POST",
                headers: { "Content-Type": "application/json" },
                body: JSON.stringify({ message: message, session_id: currentSessionId }),
            });
            const data = await response.json();
            appendMessage(data.response, "ai", new Date());
        } catch (error) {
            console.error("Error fetching AI response:", error);
            appendMessage("Sorry, I'm having trouble connecting to the AI service.", "ai", new Date());
        }
    }
    
    function appendMessage(message, sender, timestamp) {
        if (!chatHistory) return;

        const messageWrapper = document.createElement("div");
        messageWrapper.classList.add("flex", "flex-col", sender === "user" ? "items-end" : "items-start", "mb-4", "animate-fade-in");

        const bubble = document.createElement("div");
        bubble.classList.add("rounded-lg", "p-3", "max-w-lg", "shadow-sm", "text-sm", "md:text-base");

        if (sender === "user") {
            bubble.classList.add("bg-primary", "text-white");
            bubble.innerText = message;
        } else {
            bubble.classList.add("bg-white", "text-text-dark", "prose", "prose-orange", "prose-sm");
            const rawHtml = marked.parse(message);
            bubble.innerHTML = DOMPurify.sanitize(rawHtml);
        }

        let timeStr = "";
        try {
            if (timestamp instanceof Date) {
                timeStr = timestamp.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
            } else if (typeof timestamp === 'string') {
                timeStr = new Date(timestamp).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
            }
        } catch (e) { /* Ignore */ }

        const meta = document.createElement("div");
        meta.classList.add("text-xs", "text-gray-400", "mt-1", "mx-1");
        meta.innerText = timeStr;

        messageWrapper.appendChild(bubble);
        messageWrapper.appendChild(meta);

        chatHistory.appendChild(messageWrapper);
        chatHistory.scrollTop = chatHistory.scrollHeight;
    }

    // --- INITIALIZATION on Page Load ---
    if (window.activePatientId) {
        console.log(`Session active for patient ${window.activePatientId}`);
        setChatInputDisabled(false); // ENABLE chat
        if (userInput) userInput.focus();

        if (window.activeSessionId && chatHistory.children.length <= 1) { 
             console.log("Loading active session history:", window.activeSessionId);
             loadHistoryItem('chat', window.activeSessionId);
        }
    } else {
        console.log("No active patient session.");
        setChatInputDisabled(true); // DISABLE chat by default
    }
});