import { defineStore } from 'pinia' import { ref } from 'vue' import axios from 'axios' const api = axios.create({ baseURL: '/api', headers: { 'Content-Type': 'application/json', }, }) // Add auth token to requests api.interceptors.request.use((config) => { const token = localStorage.getItem('token') if (token) { config.headers.Authorization = `Bearer ${token}` } return config }) export interface Device { id: number device_uid: string hostname: string ip_address: string mac_address: string | null os_version: string | null client_version: string | null status: 'online' | 'offline' last_heartbeat: string | null registered_at: string group_name: string } export interface DeviceStatusDetail { cpu_usage: number memory_usage: number memory_total: number disk_usage: number disk_total: number running_procs: number top_processes: Array<{ name: string; pid: number; cpu_usage: number; memory_mb: number }> } export const useDeviceStore = defineStore('devices', () => { const devices = ref([]) const loading = ref(false) const total = ref(0) async function fetchDevices(params?: Record) { loading.value = true try { const { data } = await api.get('/devices', { params }) if (data.success) { devices.value = data.data.devices total.value = data.data.total ?? devices.value.length } } finally { loading.value = false } } async function fetchDeviceStatus(uid: string): Promise { const { data } = await api.get(`/devices/${uid}/status`) return data.success ? data.data : null } async function fetchDeviceHistory(uid: string, params?: Record) { const { data } = await api.get(`/devices/${uid}/history`, { params }) return data.success ? data.data : null } async function removeDevice(uid: string) { await api.delete(`/devices/${uid}`) devices.value = devices.value.filter((d) => d.device_uid !== uid) } return { devices, loading, total, fetchDevices, fetchDeviceStatus, fetchDeviceHistory, removeDevice, } })