import { ref } from "vue";
import { defineStore } from "pinia";
import { hwpodAPI } from "@/api";
import type { HwpodNodeOpsResponse, HwpodSpecsResponse } from "@/types";

export const useHwpodStore = defineStore("hwpod", () => {
  const specs = ref<HwpodSpecsResponse | null>(null);
  const nodeOps = ref<HwpodNodeOpsResponse | null>(null);
  const loading = ref(false);
  const error = ref<string | null>(null);

  async function refresh(): Promise<void> {
    loading.value = true;
    error.value = null;
    const [specsResult, nodeOpsResult] = await Promise.all([hwpodAPI.specs(), hwpodAPI.nodeOpsHealth()]);
    specs.value = specsResult.data;
    nodeOps.value = nodeOpsResult.data;
    error.value = specsResult.ok && nodeOpsResult.ok ? null : specsResult.error ?? nodeOpsResult.error ?? "HWPOD API unavailable";
    loading.value = false;
  }

  return { specs, nodeOps, loading, error, refresh };
});
