(function(){ const apiUrl = 'https://kevsono-penafort-concierge.hf.space'; let sessionId = localStorage.getItem('pf_session') || 'sess_'+Date.now(); localStorage.setItem('pf_session', sessionId); let authToken = localStorage.getItem('pf_auth_token'); // Build chat UI const win = document.createElement('div'); win.innerHTML = `
`; document.body.appendChild(win); const btn = document.getElementById('pf-btn'); const chatDiv = document.getElementById('pf-chat'); const closeBtn = document.getElementById('pf-close'); const authBtn = document.getElementById('pf-auth-btn'); const input = document.getElementById('pf-input'); const sendBtn = document.getElementById('pf-send'); const msgsDiv = document.getElementById('pf-msgs'); function addMsg(txt, role){ const d = document.createElement('div'); d.style.padding = '8px 12px'; d.style.borderRadius = '12px'; d.style.margin = '4px'; d.style.maxWidth = '80%'; d.style.wordWrap = 'break-word'; d.style.alignSelf = role==='user' ? 'flex-end' : 'flex-start'; d.style.background = role==='user' ? '#6b4b9a' : '#e9ecef'; d.style.color = role==='user' ? 'white' : 'black'; d.innerText = txt; msgsDiv.appendChild(d); msgsDiv.scrollTop = msgsDiv.scrollHeight; } async function send(){ const val = input.value.trim(); if(!val) return; addMsg(val,'user'); input.value = ''; try{ const headers = {'Content-Type':'application/json'}; if (authToken) headers['Authorization'] = 'Bearer '+authToken; const res = await fetch(apiUrl+'/api/chat',{method:'POST',headers:headers,body:JSON.stringify({message:val, session_id:sessionId})}); const data = await res.json(); addMsg(data.response,'ai'); // If token was missing but response indicates authenticated, update token if (!authToken && data.authenticated) { // token already set elsewhere } }catch(e){ addMsg('Network error','ai'); } } async function authAction(){ if(authToken){ // Logout try{ await fetch(apiUrl+'/auth/logout',{method:'POST',headers:{'Authorization':'Bearer '+authToken}}); }catch(e){} localStorage.removeItem('pf_auth_token'); authToken = null; addMsg('🔓 You have been logged out. Your future conversations will not be saved across devices.','system'); // Reload to clear any cached history setTimeout(() => window.location.reload(), 1500); }else{ // Show login/signup modal with email/password const email = prompt('Email:'); if(!email) return; const password = prompt('Password:'); if(!password) return; let action = confirm('Press OK to Login, Cancel to Register'); let endpoint = action ? '/auth/login' : '/auth/register'; try{ const res = await fetch(apiUrl+endpoint,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({email,password})}); const data = await res.json(); if(res.ok && data.access_token){ authToken = data.access_token; localStorage.setItem('pf_auth_token', authToken); addMsg('✅ Logged in successfully! Your conversations will now be saved across devices.','system'); // Refresh to load user history setTimeout(() => window.location.reload(), 1500); }else{ addMsg('Authentication failed: '+(data.detail||'Unknown error'),'system'); } }catch(e){ addMsg('Login error','system'); } } } authBtn.onclick = authAction; btn.onclick = ()=> chatDiv.style.display = 'flex'; closeBtn.onclick = ()=> chatDiv.style.display = 'none'; sendBtn.onclick = send; input.onkeypress = e => e.key==='Enter' && send(); addMsg('Hello! I am your Penafort Strategic Advisor. How can I help you today?','ai'); if(authToken) addMsg('🔐 You are logged in. Your chats are saved across devices.','system'); else addMsg('🔓 You are browsing as guest. Create an account to save your conversation history across devices.','system'); })();