/**
 * 接口测试 API 封装
 *
 * @author czj
 * @date 2026-08-17
 */

import request from '@/utils/request'

export interface AssertionRule {
  type: 'status_code' | 'body_field' | 'response_time' | 'header'
  operator: 'equals' | 'not_equals' | 'contains' | 'regex' | 'greater_than' | 'less_than'
  value: any
  path?: string
}

export interface ApiRequestConfig {
  method: string
  url: string
  headers: Record<string, string>
  body?: any
  auth_required: boolean
  sign_request: boolean
  account_key: string
  assertions: AssertionRule[]
  description: string
}

export interface ApiTestExecuteRequest {
  case_ids: string[]
  config_id?: string
  target_url?: string
  name?: string
}

export const apiTestApi = {
  /** 获取接口测试用例列表 */
  listCases(params?: {
    skip?: number
    limit?: number
    module_id?: string
    keyword?: string
  }) {
    return request.get('/api/api-test/cases', { params }) as any
  },

  /** 获取用例详情 */
  getCase(caseId: string) {
    return request.get(`/api/api-test/cases/${caseId}`) as any
  },

  /** 创建执行记录 */
  createExecution(data: ApiTestExecuteRequest) {
    return request.post('/api/api-test/executions', data) as any
  },

  /** 执行接口测试 */
  runExecution(executionId: string) {
    return request.post(`/api/api-test/executions/${executionId}/run`) as any
  },

  /** 获取执行记录列表 */
  listExecutions(params?: { skip?: number; limit?: number }) {
    return request.get('/api/api-test/executions', { params }) as any
  },

  /** 获取执行结果 */
  getExecutionResults(executionId: string, params?: { skip?: number; limit?: number }) {
    return request.get(`/api/api-test/executions/${executionId}/results`, { params }) as any
  },

  /** 获取单个用例执行详情 */
  getCaseResultDetail(executionId: string, caseResultId: string) {
    return request.get(`/api/api-test/executions/${executionId}/result/${caseResultId}`) as any
  },
}