/**
 * Health Checker — 健康检查器
 *
 * @see docs/生产级通用 AI Agent 智能体桌面应用：完整设计与构建指南.html — 第十一章
 */

import { existsSync } from 'fs';
import { join } from 'path';
import { app } from 'electron';

export interface HealthReport {
  healthy: boolean;
  checks: HealthCheck[];
  timestamp: Date;
}

export interface HealthCheck {
  name: string;
  healthy: boolean;
  latencyMs?: number;
  error?: string;
}

export class HealthChecker {
  constructor(private dbPath?: string) {}

  async check(): Promise<HealthReport> {
    const checks: HealthCheck[] = [];
    checks.push(await this.checkDatabase());
    checks.push(await this.checkDiskSpace());
    checks.push(await this.checkMemoryUsage());
    const allHealthy = checks.every((c) => c.healthy);
    return { healthy: allHealthy, checks, timestamp: new Date() };
  }

  private async checkDatabase(): Promise<HealthCheck> {
    const start = Date.now();
    try {
      if (this.dbPath && existsSync(this.dbPath)) {
        return { name: 'database', healthy: true, latencyMs: Date.now() - start };
      }
      return { name: 'database', healthy: false, error: 'Database file not found' };
    } catch (error) {
      return { name: 'database', healthy: false, error: (error instanceof Error ? error.message : String(error)) };
    }
  }

  private async checkDiskSpace(): Promise<HealthCheck> {
    try {
      const userDataPath = app.getPath('userData');
      // 简单检查：userData 目录是否存在且可写
      if (existsSync(userDataPath)) {
        return { name: 'disk_space', healthy: true };
      }
      return { name: 'disk_space', healthy: false, error: 'User data path not accessible' };
    } catch (error) {
      return { name: 'disk_space', healthy: false, error: (error instanceof Error ? error.message : String(error)) };
    }
  }

  private async checkMemoryUsage(): Promise<HealthCheck> {
    const used = process.memoryUsage();
    const heapUsedMB = used.heapUsed / 1024 / 1024;
    const thresholdMB = 512;
    return {
      name: 'memory_usage',
      healthy: heapUsedMB < thresholdMB,
      ...(heapUsedMB >= thresholdMB ? { error: `Heap usage ${heapUsedMB.toFixed(0)}MB exceeds threshold ${thresholdMB}MB` } : {}),
    };
  }
}
