import { state } from './state.js';
import * as api from './api.js';
import * as ui from './ui.js';
export async function fetchPatients() {
try {
const patients = await api.fetchPatientsList();
if (patients.length > 0) {
state.patientId = patients[0].patient_id;
await showDashboard(state.patientId);
} else {
// This should never happen now since the backend auto-creates a profile, but just in case:
ui.showAlert("Profile Error", "No patient profile found. Please contact support.");
}
} catch (err) {
console.error("Failed to load patient profile:", err);
}
}
/**
* Fetches patient dashboard data (past sessions, domains, etc.) and renders the dashboard screen.
*
* @param {string} pId - The patient ID to load dashboard for.
*/
export async function showDashboard(pId) {
try {
const data = await api.loadDashboard(pId);
document.getElementById("dashboardPatientName").textContent = data.patient.name;
document.getElementById("dashboardPatientAge").textContent = data.patient.age ? `Age: ${data.patient.age}` : "Age: --";
const list = document.getElementById("dashboardSessionsList");
list.innerHTML = "";
const dashStartBtn = document.getElementById("dashboardStartBtn");
dashStartBtn.textContent = "NEW SESSION";
if (data.sessions && data.sessions.length > 0) {
data.sessions.forEach(sess => {
const d = new Date(sess.created_at).toLocaleString();
const summary = sess.rolling_summary || "No summary available.";
list.innerHTML += `
${d}
▼
No previous sessions found.
`; } const domainsDiv = document.getElementById("dashboardDomains"); if (domainsDiv && data.profile) { domainsDiv.innerHTML = `${data.profile.risk_assessment}