// {{ display_name }} MCP server — generated by mcpify. Do not hand-edit.

export type ComponentStatus = 'healthy' | 'degraded' | 'unhealthy';

export interface ComponentState {
  name: string;
  critical: boolean;
  status: ComponentStatus;
  lastCheckedAt?: Date;
  lastError?: string;
}

/**
 * Tracks the health of every registered component, distinguishing critical
 * components (a failure here makes the whole server unhealthy) from
 * optional ones (a failure only degrades it) — REQ-2.3.4.
 */
export class ComponentRegistry {
  private readonly components = new Map<string, ComponentState>();

  register(name: string, critical: boolean): void {
    this.components.set(name, { name, critical, status: 'healthy' });
  }

  report(name: string, status: ComponentStatus, error?: string): void {
    const existing = this.components.get(name);
    this.components.set(name, {
      name,
      critical: existing?.critical ?? false,
      status,
      lastCheckedAt: new Date(),
      lastError: error,
    });
  }

  overallStatus(): ComponentStatus {
    const states = [...this.components.values()];
    if (states.some((component) => component.critical && component.status === 'unhealthy')) {
      return 'unhealthy';
    }
    if (states.some((component) => component.status !== 'healthy')) {
      return 'degraded';
    }
    return 'healthy';
  }

  snapshot(): ComponentState[] {
    return [...this.components.values()];
  }
}
