/**
 * 开发调试全链路日志 — TypeScript 类型定义。
 *
 * 与后端 trace 模块的 Pydantic 模型和 JSON Lines 输出结构对应。
 */

// ---- Span 类型枚举 ----

export type SpanType =
  | "HTTP_IN" | "AUTH" | "ROUTE" | "SERVICE"
  | "DB_QUERY" | "DB_CONN" | "DB_CONN_RELEASE"
  | "HTTP_OUT" | "ERROR" | "DB_ERROR"
  | "MIDDLEWARE" | "MIDDLEWARE_ERROR"
  | "SSE_START" | "SSE_EVENT" | "SSE_END"
  | "AI_CALL" | "AI_STREAM" | "AI_ERROR"
  | "WS_CONNECT" | "WS_MESSAGE" | "WS_DISCONNECT"
  | "JOB_START" | "JOB_END" | "JOB_ERROR";

export type TraceType = "http" | "sse" | "ws" | "job";

// ---- 数据模型 ----

export interface TraceSpan {
  span_type: SpanType;
  module: string;
  function: string;
  description_zh: string;
  description_en: string;
  params: Record<string, unknown>;
  result_summary: string;
  duration_ms: number;
  timestamp: string;
  extra: Record<string, unknown>;
}

export interface TraceRequest {
  request_id: string;
  trace_type: TraceType;
  timestamp: string;
  method: string;
  path: string;
  status_code: number | null;
  total_duration_ms: number;
  user_id: number | null;
  site_id: number | null;
  db_query_count: number;
  db_total_ms: number;
  error: string | null;
  span_count: number;
}

export interface TraceDetail extends Omit<TraceRequest, "span_count"> {
  spans: TraceSpan[];
}

export interface TraceSettings {
  enabled: boolean;
  log_dir: string;
  retention_days: number;
  log_sql: boolean;
  log_params: boolean;
}

export interface TraceFilter {
  date: string;
  start_time?: string;
  end_time?: string;
  trace_type?: TraceType;
  method?: string;
  path_contains?: string;
  status_code?: number;
  min_duration?: number;
  has_error?: boolean;
  span_type?: SpanType;
  page?: number;
  page_size?: number;
}

export interface CoverageCategory {
  total: number;
  covered: number;
  uncovered: string[];
}

export interface TraceCoverage {
  scan_time: string;
  routes: CoverageCategory;
  services: CoverageCategory;
  jobs: CoverageCategory;
  sse_endpoints: CoverageCategory;
  ws_endpoints: CoverageCategory;
}
