/**
 * Monitoring API Service
 */

import type {
  CacheStats,
  DatabaseStats,
  ProxyStats,
  QueueStats,
  ServiceStatus,
  SystemHealth,
  SystemOverview,
} from '../types';

const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:2003';
const MONITORING_BASE = `${API_BASE_URL}/api/system/monitoring`;

export const monitoringApi = {
  /**
   * Get overall system health
   */
  async getSystemHealth(): Promise<SystemHealth> {
    const response = await fetch(MONITORING_BASE);
    if (!response.ok) {
      throw new Error(`Failed to fetch system health: ${response.statusText}`);
    }
    return response.json();
  },

  /**
   * Get cache statistics
   */
  async getCacheStats(): Promise<CacheStats> {
    const response = await fetch(`${MONITORING_BASE}/cache`);
    if (!response.ok) {
      throw new Error(`Failed to fetch cache stats: ${response.statusText}`);
    }
    return response.json();
  },

  /**
   * Get queue statistics
   */
  async getQueueStats(): Promise<{ queues: QueueStats[] }> {
    const response = await fetch(`${MONITORING_BASE}/queues`);
    if (!response.ok) {
      throw new Error(`Failed to fetch queue stats: ${response.statusText}`);
    }
    return response.json();
  },

  /**
   * Get specific queue statistics
   */
  async getQueueStatsByName(name: string): Promise<QueueStats> {
    const response = await fetch(`${MONITORING_BASE}/queues/${name}`);
    if (!response.ok) {
      throw new Error(`Failed to fetch queue ${name} stats: ${response.statusText}`);
    }
    return response.json();
  },

  /**
   * Get database statistics
   */
  async getDatabaseStats(): Promise<{ databases: DatabaseStats[] }> {
    const response = await fetch(`${MONITORING_BASE}/databases`);
    if (!response.ok) {
      throw new Error(`Failed to fetch database stats: ${response.statusText}`);
    }
    return response.json();
  },

  /**
   * Get specific database statistics
   */
  async getDatabaseStatsByType(type: 'postgres' | 'mongodb' | 'questdb'): Promise<DatabaseStats> {
    const response = await fetch(`${MONITORING_BASE}/databases/${type}`);
    if (!response.ok) {
      throw new Error(`Failed to fetch ${type} stats: ${response.statusText}`);
    }
    return response.json();
  },

  /**
   * Get detailed cache info
   */
  async getCacheInfo(): Promise<{ parsed: CacheStats; raw: string }> {
    const response = await fetch(`${MONITORING_BASE}/cache/info`);
    if (!response.ok) {
      throw new Error(`Failed to fetch cache info: ${response.statusText}`);
    }
    return response.json();
  },

  /**
   * Get service status
   */
  async getServiceStatus(): Promise<{ services: ServiceStatus[] }> {
    const response = await fetch(`${MONITORING_BASE}/services`);
    if (!response.ok) {
      throw new Error(`Failed to fetch service status: ${response.statusText}`);
    }
    return response.json();
  },

  /**
   * Get proxy statistics
   */
  async getProxyStats(): Promise<ProxyStats> {
    const response = await fetch(`${MONITORING_BASE}/proxies`);
    if (!response.ok) {
      throw new Error(`Failed to fetch proxy stats: ${response.statusText}`);
    }
    return response.json();
  },

  /**
   * Get system overview
   */
  async getSystemOverview(): Promise<SystemOverview> {
    const response = await fetch(`${MONITORING_BASE}/overview`);
    if (!response.ok) {
      throw new Error(`Failed to fetch system overview: ${response.statusText}`);
    }
    return response.json();
  },
};
