/**
 * 性能测试模块类型定义
 *
 * @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 type PerfProjectType = 'standard' | 'custom'

/** 性能测试任务 */
export interface PerformanceTask {
  id: string
  name: string
  description: string | null
  status: PerfStatus
  errorMessage: string | null
  projectId: 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
  p95ResponseTime: number | null
  p99ResponseTime: number | null
  stdDev: number | null
  latencyAvg: number | null
  latencyMin: number | null
  latencyMax: number | null
  connectTimeAvg: number | null
  connectTimeMax: number | null
  peakTps: number | null
  totalSentBytes: number | null
  totalReceivedBytes: number | null
  apdex: number | null
  errorTypeTimeout: number | null
  errorTypeConnectError: number | null
  errorTypeClientError: number | null
  errorTypeHttp4xx: number | null
  errorTypeHttp5xx: number | null
  errorTypeAssertion: 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
  projectId?: 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
  projectId?: 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
  projectId: string | null
  targetUrl: string
  method: string
  mode: PerfMode
  concurrency: number
  duration: number
  actualTps: number | null
  peakTps: number | null
  p95ResponseTime: number | null
  avgResponseTime: 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
  p95ResponseTime: number
  p99ResponseTime: number
  stdDev: number
  latencyAvg: number
  connectTimeAvg: number
  sentBytes: number
  receivedBytes: number
  peakTps: 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
  peakTps: number
  minResponseTime: number
  maxResponseTime: number
  avgResponseTime: number
  p50ResponseTime: number
  p90ResponseTime: number
  p95ResponseTime: number
  p99ResponseTime: number
  stdDev: number
  latencyAvg: number
  latencyMin: number
  latencyMax: number
  connectTimeAvg: number
  connectTimeMax: number
  apdex: number
  totalSentBytes: number
  totalReceivedBytes: number
  sentKbPerSec: number
  receivedKbPerSec: number
  status2xx: number
  status3xx: number
  status4xx: number
  status5xx: number
  errorTypeTimeout: number
  errorTypeConnectError: number
  errorTypeClientError: number
  errorTypeHttp4xx: number
  errorTypeHttp5xx: number
  errorTypeAssertion: 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
    p95: number
    p99: number
    stdDev: number
    latencyAvg: number
    connectTimeAvg: number
    sentBytes: number
    receivedBytes: number
    peakTps: number
    successCount: number
    failCount: number
    totalRequests: number
    concurrentUsers: 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
  credentialName: string | null
  loginUsername: string | null
  loginPassword: string | null
  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
  credentialName?: string | null
  loginUsername?: string | null
  loginPassword?: string | null
  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[]
}

// ==================== 性能测试项目 ====================

/** 性能测试项目 */
export interface PerformanceProject {
  id: string
  name: string
  description: string
  projectType: PerfProjectType
  icon: string
  sortOrder: number
  taskCount: number
  lastRunStatus: string | null
  lastRunTime: string | null
  createdAt: string | null
  updatedAt: string | null
}

/** 创建项目请求 */
export interface PerformanceProjectCreate {
  name: string
  description?: string
  projectType?: PerfProjectType
  icon?: string
  sortOrder?: number
}

/** 更新项目请求 */
export interface PerformanceProjectUpdate {
  name?: string
  description?: string
  projectType?: PerfProjectType
  icon?: string
  sortOrder?: number
}

/** 项目列表响应 */
export interface PerformanceProjectListResponse {
  total: number
  items: PerformanceProject[]
}

/** 项目统计 */
export interface PerformanceProjectStats {
  projectId: string
  taskCount: number
  statusDistribution: Record<string, number>
  lastRunStatus: string | null
  lastRunTime: string | null
}

// ==================== 批量执行 ====================

/** 批量执行请求 */
export interface BatchRunRequest {
  taskIds: string[]
  projectId?: string | null
}

/** 批量执行响应 */
export interface BatchRunResponse {
  message: string
  batchId: string
  taskCount: number
}

/** WebSocket 批量执行进度消息 */
export interface PerfBatchProgress {
  type: 'perf_batch_progress'
  batchId: string
  projectId: string | null
  currentIndex: number
  totalCount: number
  currentTaskId: string
  currentTaskName: string
  currentStatus: 'running' | 'completed' | 'failed'
  successCount: number
  failCount: number
}