import type { ServiceContract } from '../ServiceContract';

export const IHealthService: ServiceContract = {
  id: 'health',
  description: 'Health status aggregation — error rates, latency percentiles',
  ops: {
    'health.status': {
      description: 'Get aggregated health status across all components',
      payload: {},
      response: {
        ok: { type: 'boolean' },
        status: { type: 'string', description: 'healthy|degraded|unhealthy' },
        window: { type: 'string' },
        totalRecords: { type: 'number' },
        totalErrors: { type: 'number' },
        errorsBySource: { type: 'Record<string, number>' },
        p50Ms: { type: 'number | null' },
        p95Ms: { type: 'number | null' },
      },
    },
    'health.component': {
      description: 'Get health status for a specific component',
      payload: {
        mount: { type: 'string', required: true, description: 'Component mount path' },
      },
      response: {
        ok: { type: 'boolean' },
        mount: { type: 'string' },
        status: { type: 'string' },
        errorCount: { type: 'number' },
        errorRate: { type: 'number' },
      },
      errors: [{ code: 'MISSING_MOUNT', description: 'mount is required' }],
    },
    'health.setThresholds': {
      description: 'Configure health thresholds and time window',
      payload: {
        errorRateWarning: { type: 'number', required: false },
        errorRateCritical: { type: 'number', required: false },
        windowMs: { type: 'number', required: false },
      },
      response: { ok: { type: 'boolean' }, thresholds: { type: 'object' } },
    },
    'health.getThresholds': {
      description: 'Get current health threshold configuration',
      payload: {},
      response: { ok: { type: 'boolean' }, thresholds: { type: 'object' } },
    },
  },
};
