/**
 * 性能监控中间件
 * 监控 API 响应时间、慢查询、错误率
 */

interface PerformanceMetrics {
  totalRequests: number;
  avgResponseTime: number;
  slowRequests: number;
  errors: number;
  endpoints: Record<string, {
    count: number;
    avgTime: number;
    maxTime: number;
    errors: number;
  }>;
}

const metrics: PerformanceMetrics = {
  totalRequests: 0,
  avgResponseTime: 0,
  slowRequests: 0,
  errors: 0,
  endpoints: {},
};

const SLOW_THRESHOLD = 1000; // 1 秒视为慢请求

export function performanceMonitor() {
  return async (ctx: any, next: any) => {
    const start = Date.now();
    const endpoint = `${ctx.method} ${ctx.path}`;

    // 初始化端点指标
    if (!metrics.endpoints[endpoint]) {
      metrics.endpoints[endpoint] = {
        count: 0,
        avgTime: 0,
        maxTime: 0,
        errors: 0,
      };
    }

    try {
      await next();

      const duration = Date.now() - start;
      
      // 更新指标
      metrics.totalRequests++;
      metrics.avgResponseTime = 
        (metrics.avgResponseTime * (metrics.totalRequests - 1) + duration) / metrics.totalRequests;

      const epMetrics = metrics.endpoints[endpoint];
      epMetrics.count++;
      epMetrics.avgTime = (epMetrics.avgTime * (epMetrics.count - 1) + duration) / epMetrics.count;
      epMetrics.maxTime = Math.max(epMetrics.maxTime, duration);

      // 慢请求日志
      if (duration > SLOW_THRESHOLD) {
        metrics.slowRequests++;
        console.warn(`[Performance] 慢请求: ${endpoint} - ${duration}ms`);
      }

      // 添加响应头
      ctx.set('X-Response-Time', `${duration}ms`);

    } catch (error) {
      metrics.errors++;
      if (metrics.endpoints[endpoint]) {
        metrics.endpoints[endpoint].errors++;
      }
      throw error;
    }
  };
}

/**
 * 获取性能指标
 */
export function getPerformanceMetrics(): PerformanceMetrics {
  return { ...metrics };
}

/**
 * 重置性能指标
 */
export function resetPerformanceMetrics(): void {
  metrics.totalRequests = 0;
  metrics.avgResponseTime = 0;
  metrics.slowRequests = 0;
  metrics.errors = 0;
  metrics.endpoints = {};
}

/**
 * 性能指标路由
 */
export async function getMetrics(ctx: any) {
  ctx.body = {
    code: 0,
    data: {
      ...metrics,
      errorRate: metrics.totalRequests > 0 
        ? ((metrics.errors / metrics.totalRequests) * 100).toFixed(2) + '%'
        : '0%',
    },
  };
}
