/**
 * useServerList composable
 *
 * Fetches a flat server list (id + name) for dropdowns/selects.
 * Eliminates the identical loadServers() pattern in 5+ pages.
 */
import { ref } from 'vue'
import { http } from '@/api'

export interface ServerBrief {
  id: number
  name: string
  domain?: string
  is_online?: boolean
  status?: string
}

export function useServerList() {
  const servers = ref<ServerBrief[]>([])
  const loading = ref(false)

  async function loadServers(opts?: { perPage?: number }) {
    loading.value = true
    try {
      const res = await http.get<{ items: ServerBrief[] }>('/servers/', {
        per_page: opts?.perPage ?? 200,
      })
      servers.value = res.items || []
    } catch {
      servers.value = []
    } finally {
      loading.value = false
    }
  }

  return { servers, loading, loadServers }
}
