async function loadSettings() { const data = await apiFetch("/api/settings"); if (!data) return; const s = data.settings || {}; if (s.theme) applyTheme(s.theme); const authData = await apiFetch("/api/auth/me"); if (authData && authData.user) { document.getElementById("profileName").innerText = authData.user.username; document.getElementById("profileEmail").value = authData.user.email || ""; } loadMemories(); loadModels(); } async function loadMemories() { const data = await apiFetch("/api/memory"); if (!data) return; const list = document.getElementById("memoryList"); if (!list) return; list.innerHTML = ""; (data.memories || []).forEach(m => { const div = document.createElement("div"); div.className = "memory-item"; div.innerHTML = `${m.key}: ${m.value} `; list.appendChild(div); }); } async function addMemory() { const key = document.getElementById("memKey")?.value.trim(); const value = document.getElementById("memValue")?.value.trim(); if (!key || !value) return alert("Enter both key and value"); await apiFetch("/api/memory", { method: "POST", body: JSON.stringify({key, value}) }); document.getElementById("memKey").value = ""; document.getElementById("memValue").value = ""; loadMemories(); } async function deleteMemory(id) { await apiFetch("/api/memory/" + id, {method: "DELETE"}); loadMemories(); } async function searchMemory() { const q = document.getElementById("memSearch")?.value.trim(); const data = await apiFetch("/api/memory/search?q=" + encodeURIComponent(q)); if (!data) return; const list = document.getElementById("memoryList"); if (!list) return; list.innerHTML = ""; (data.memories || []).forEach(m => { const div = document.createElement("div"); div.className = "memory-item"; const strong = document.createElement("strong"); strong.textContent = m.key + ": "; div.appendChild(strong); div.appendChild(document.createTextNode(m.value)); const delBtn = document.createElement("button"); delBtn.textContent = "✕"; delBtn.style.cssText = "margin-left:10px;background:none;border:none;color:#ef4444;cursor:pointer"; delBtn.onclick = () => deleteMemory(m.id); div.appendChild(delBtn); list.appendChild(div); }); } async function loadModels() { const data = await apiFetch("/api/settings/models"); if (!data) return; const sel = document.getElementById("modelSelect"); if (!sel) return; sel.innerHTML = ""; (data.models || []).forEach(m => { const opt = document.createElement("option"); opt.value = m.id; opt.innerText = m.name; sel.appendChild(opt); }); } async function updateModel() { const model = document.getElementById("modelSelect")?.value; if (model) { await apiFetch("/api/settings", { method: "PUT", body: JSON.stringify({ai_model: model}) }); alert("Model updated. New chats will use " + model); } } function applyTheme(theme) { document.body.className = theme || "dark"; localStorage.setItem("nexus_theme", theme); } function setTheme(theme) { applyTheme(theme); apiFetch("/api/settings", { method: "PUT", body: JSON.stringify({theme}) }); } async function updateProfile() { const email = document.getElementById("profileEmail")?.value; await apiFetch("/api/settings/profile", { method: "PUT", body: JSON.stringify({email}) }); alert("Profile updated"); } async function deleteAccount() { if (!confirm("Are you sure? This cannot be undone.")) return; await fetch(API + "/api/auth/logout", {method: "POST", credentials: "include", headers: {"X-CSRF-Token": getCsrfToken()}}); window.location.href = "/login"; } window.onload = async function() { const auth = await checkAuth(); if (auth) loadSettings(); };