import { create } from 'zustand'; import * as healthApi from '../services/health'; interface HealthState { todaySummary: healthApi.TodaySummary | null; trendData: Record; loading: boolean; refreshToday: () => Promise; getTrend: (indicator: string, range: string) => Promise<{ date: string; value: number }[]>; } export const useHealthStore = create((set, get) => ({ todaySummary: null, trendData: {}, loading: false, refreshToday: async () => { set({ loading: true }); try { const data = await healthApi.getTodaySummary(); set({ todaySummary: data, loading: false }); } catch { set({ loading: false }); } }, getTrend: async (indicator: string, range: string) => { const cacheKey = `${indicator}_${range}`; const cached = get().trendData[cacheKey]; if (cached) return cached; try { const resp = await healthApi.getTrend(indicator, range); const points = resp.data_points || []; set((s) => ({ trendData: { ...s.trendData, [cacheKey]: points } })); return points; } catch { return []; } }, }));