/**
 * 性能测试模块类型定义
 *
 * @author czj
 * @date 2026-08-12
 */

/** 任务状态 */
export type PerfStatus = 'pending' | 'running' | 'completed' | 'failed' | 'cancelled'

/** 压测模式 */
export type PerfMode = 'concurrent' | 'qps' | 'step'

/** 断言类型 */
export type AssertionType = 'status_code' | 'response_time' | 'response_body'

/** 断言操作符 */
export type AssertionOperator = 'eq' | 'neq' | 'gt' | 'gte' | 'lt' | 'lte' | 'contains' | 'not_contains' | 'regex'

/** 断言规则 */
export interface AssertionRule {
  type: AssertionType
  operator: AssertionOperator
  value: string
  path?: string | null
  description?: string | null
}

/** 唯一性字段规则 */
export interface UniqueFieldRule {
  field: string
  strategy: 'suffix' | 'template'
  template?: string | null
}

/** 性能测试任务 */
export interface PerformanceTask {
  id: string
  name: string
  description: string | null
  status: PerfStatus
  errorMessage: string | null
  targetUrl: string
  method: string
  headers: Record<string, string> | null
  body: string | null
  bodyTemplate: string | null
  mode: PerfMode
  concurrency: number
  qpsTarget: number | null
  duration: number
  rampUp: number | null
  stepConcurrency: number | null
  stepDuration: number | null
  authRequired: boolean
  accountKey: string | null
  loginUsername: string | null
  loginPassword: string | null
  signRequest: boolean
  assertions: AssertionRule[]
  totalRequests: number | null
  successCount: number | null
  failCount: number | null
  minResponseTime: number | null
  maxResponseTime: number | null
  avgResponseTime: number | null
  p50ResponseTime: number | null
  p90ResponseTime: number | null
  p99ResponseTime: number | null
  actualTps: number | null
  errorRate: number | null
  startTime: string | null
  endTime: string | null
  durationActual: number | null
  presetId: string | null
  captureRules: any[] | null
  uniqueFields: UniqueFieldRule[] | null
  createdAt: string
  updatedAt: string
}

/** 创建任务请求 */
export interface PerformanceTaskCreate {
  name: string
  description?: string | null
  targetUrl: string
  method?: string
  headers?: Record<string, string> | null
  body?: string | null
  bodyTemplate?: string | null
  mode: PerfMode
  concurrency?: number
  qpsTarget?: number | null
  duration: number
  rampUp?: number | null
  stepConcurrency?: number | null
  stepDuration?: number | null
  authRequired?: boolean
  accountKey?: string | null
  loginUsername?: string | null
  loginPassword?: string | null
  signRequest?: boolean
  assertions?: AssertionRule[]
  uniqueFields?: UniqueFieldRule[] | null
}

/** 更新任务请求 */
export interface PerformanceTaskUpdate {
  name?: string
  description?: string | null
  targetUrl?: string
  method?: string
  headers?: Record<string, string> | null
  body?: string | null
  bodyTemplate?: string | null
  mode?: PerfMode
  concurrency?: number
  qpsTarget?: number | null
  duration?: number
  rampUp?: number | null
  stepConcurrency?: number | null
  stepDuration?: number | null
  authRequired?: boolean
  accountKey?: string | null
  loginUsername?: string | null
  loginPassword?: string | null
  signRequest?: boolean
  assertions?: AssertionRule[]
  uniqueFields?: UniqueFieldRule[] | null
}

/** 任务列表项 */
export interface PerformanceTaskListItem {
  id: string
  name: string
  description: string | null
  status: PerfStatus
  targetUrl: string
  method: string
  mode: PerfMode
  concurrency: number
  duration: number
  actualTps: number | null
  errorRate: number | null
  startTime: string | null
  endTime: string | null
  createdAt: string
}

/** 任务列表响应 */
export interface PerformanceTaskListResponse {
  total: number
  items: PerformanceTaskListItem[]
}

/** 执行请求 */
export interface PerformanceRunRequest {
  configOverride?: Record<string, any> | null
}

/** 执行响应 */
export interface PerformanceRunResponse {
  message: string
  taskId: string
  status: string
}

/** 快照数据 */
export interface PerformanceSnapshot {
  id: number
  taskId: string
  elapsed: number
  concurrentUsers: number
  tps: number
  avgResponseTime: number
  minResponseTime: number
  maxResponseTime: number
  p50ResponseTime: number
  p90ResponseTime: number
  p99ResponseTime: number
  successCount: number
  failCount: number
  status2xx: number
  status3xx: number
  status4xx: number
  status5xx: number
}

/** 快照列表响应 */
export interface PerformanceSnapshotListResponse {
  total: number
  items: PerformanceSnapshot[]
}

/** 报告摘要 */
export interface PerformanceReportSummary {
  taskId: string
  taskName: string
  targetUrl: string
  method: string
  mode: string
  concurrency: number
  durationSetting: number
  durationActual: number | null
  startTime: string | null
  endTime: string | null
  accountKey: string | null
}

/** 报告指标 */
export interface PerformanceReportMetrics {
  totalRequests: number
  successCount: number
  failCount: number
  errorRate: number
  actualTps: number
  minResponseTime: number
  maxResponseTime: number
  avgResponseTime: number
  p50ResponseTime: number
  p90ResponseTime: number
  p99ResponseTime: number
  status2xx: number
  status3xx: number
  status4xx: number
  status5xx: number
}

/** 报告响应 */
export interface PerformanceReportResponse {
  summary: PerformanceReportSummary
  metrics: PerformanceReportMetrics
  snapshots: PerformanceSnapshot[]
}

/** WebSocket 快照消息 */
export interface PerfWsSnapshot {
  type: 'perf_snapshot'
  taskId: string
  data: {
    elapsed: number
    tps: number
    avgResponseTime: number
    p50: number
    p90: number
    p99: number
    successCount: number
    failCount: number
    totalRequests: number
    status2xx: number
    status4xx: number
    status5xx: number
  }
  timestamp: string
}

/** WebSocket 完成消息 */
export interface PerfWsComplete {
  type: 'perf_complete'
  taskId: string
  status: string
  data: Record<string, any>
}

// ==================== API 接口预设 ====================

/** 接口预设 */
export interface ApiPreset {
  id: string
  name: string
  description: string
  method: string
  targetUrl: string
  headers: Record<string, string> | null
  body: any | null
  bodyTemplate: any | null
  authRequired: boolean
  signRequest: boolean
  accountKey: string
  captureRules: any[] | null
  createdAt: string | null
  updatedAt: string | null
}

/** 创建接口预设请求 */
export interface ApiPresetCreate {
  name: string
  description?: string
  method?: string
  targetUrl: string
  headers?: Record<string, string> | null
  body?: any | null
  bodyTemplate?: any | null
  authRequired?: boolean
  signRequest?: boolean
  accountKey?: string
  captureRules?: any[] | null
}

/** 接口预设列表项 */
export interface ApiPresetListItem {
  id: string
  name: string
  description: string
  method: string
  targetUrl: string
  updatedAt: string | null
}

/** 接口预设列表响应 */
export interface ApiPresetListResponse {
  total: number
  items: ApiPresetListItem[]
}

// ==================== curl 解析 ====================

/** curl 解析结果 */
export interface CurlParseResult {
  method: string
  targetUrl: string
  headers: Record<string, string>
  body: any | null
  authRequired: boolean
  signRequest: boolean
  cookie: string | null
  errors: string[]
}