import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useApi } from '../composables/useApi.js'

export const useSystemStore = defineStore('system', () => {
  const api = useApi()
  const healthy = ref(null)  // null=unknown, true=ok, false=down
  const stats = ref(null)
  const statsLoading = ref(false)

  async function refreshHealth() {
    try {
      const body = await api.get('/api/health')
      healthy.value = body.status === 'ok'
    } catch {
      healthy.value = false
    }
  }

  async function refreshStats() {
    statsLoading.value = true
    try {
      stats.value = await api.get('/api/system/stats')
    } finally {
      statsLoading.value = false
    }
  }

  return { healthy, stats, statsLoading, refreshHealth, refreshStats }
})
