/** System monitoring API client — /api/v1/health, /api/v1/metrics, /api/v1/system */

import { BaseApiClient } from './base'

const API_BASE = '/api/v1'

export interface IHealthCheck {
  status: string
  version: string
  checks: Record<string, unknown>
}

export interface IMetricsData {
  tasks: {
    total_tasks: number
    completed_tasks: number
    failed_tasks: number
    pending_tasks: number
  }
  agents: {
    total_agents: number
  }
  skills: {
    total_skills: number
  }
  version: string
}

export interface ISystemResources {
  cpu: {
    count: number
    load_average: number[] | null
  }
  memory: {
    total_bytes: number
    used_bytes: number
    free_bytes: number
    percent: number | null
  }
  disk: {
    total_bytes: number
    used_bytes: number
    free_bytes: number
    percent: number | null
  }
  timestamp: number
}

class SystemApiClient extends BaseApiClient {
  constructor(baseUrl: string = API_BASE) {
    super(baseUrl)
  }

  async getHealth(): Promise<IHealthCheck> {
    return this.request<IHealthCheck>('/health')
  }

  async getMetrics(): Promise<IMetricsData> {
    return this.request<IMetricsData>('/metrics')
  }

  async getResources(): Promise<ISystemResources> {
    return this.request<ISystemResources>('/system/resources')
  }
}

export const systemApi = new SystemApiClient()

export const getHealth = systemApi.getHealth.bind(systemApi)
export const getMetrics = systemApi.getMetrics.bind(systemApi)
export const getResources = systemApi.getResources.bind(systemApi)
