/**
 * 安全测试 API 封装
 *
 * @author czj
 * @date 2026-07-21
 */

import request from '@/utils/request'
import type {
  SecurityConfig,
  SecurityConfigFormData,
  VulnerabilityResult,
  SecurityExecutionCreate,
  SecurityExecutionResponse,
  KnowledgeBaseResponse,
  AccountTestResponse,
  ExecutionSummary,
} from '@/types/security'

// ==================== 配置管理 ====================

/** 获取安全测试配置列表 */
export function getSecurityConfigs(skip = 0, limit = 20) {
  return request.get<{ items: SecurityConfig[]; total: number }>('/api/security/config', {
    params: { skip, limit }
  })
}

/** 获取默认配置 */
export function getDefaultConfig() {
  return request.get<SecurityConfig>('/api/security/config/default')
}

/** 获取指定配置 */
export function getSecurityConfig(configId: string) {
  return request.get<SecurityConfig>(`/api/security/config/${configId}`)
}

/** 创建安全测试配置 */
export function createSecurityConfig(data: SecurityConfigFormData) {
  return request.post<SecurityConfig>('/api/security/config', data)
}

/** 更新安全测试配置 */
export function updateSecurityConfig(configId: string, data: Partial<SecurityConfigFormData>) {
  return request.put<SecurityConfig>(`/api/security/config/${configId}`, data)
}

/** 删除安全测试配置 */
export function deleteSecurityConfig(configId: string) {
  return request.delete(`/api/security/config/${configId}`)
}

// ==================== 账号测试 ====================

/** 测试账号连接 */
export function testAccount(configId: string, accountKey: string) {
  return request.post<AccountTestResponse>('/api/security/test-account', {
    configId,
    accountKey
  })
}

// ==================== 执行管理 ====================

/** 创建安全测试执行 */
export function createSecurityExecution(data: SecurityExecutionCreate) {
  return request.post<SecurityExecutionResponse>('/api/security/executions', data)
}

/** 触发执行 */
export function runSecurityExecution(executionId: string) {
  return request.post<{ execution_id: string; status: string; message: string }>(
    `/api/security/executions/${executionId}/run`
  )
}

/** 获取执行结果 */
export function getSecurityResults(
  executionId: string,
  params?: { skip?: number; limit?: number; level?: string; isVulnerable?: boolean }
) {
  return request.get<{ items: VulnerabilityResult[]; total: number }>(
    `/api/security/executions/${executionId}/results`,
    { params }
  )
}

/** 获取执行摘要 */
export function getSecuritySummary(executionId: string) {
  return request.get<ExecutionSummary>(
    `/api/security/executions/${executionId}/summary`
  )
}

/** 获取安全测试报告内容（Markdown） */
export function getSecurityReport(executionId: string) {
  return request.get<{ execution_id: string; content: string; format: string }>(
    `/api/security/executions/${executionId}/report`
  )
}

/** 安全测试报告下载地址 */
export function getSecurityReportDownloadUrl(executionId: string): string {
  return `/api/security/executions/${executionId}/report/download`
}

// ==================== 知识库 ====================

/** 获取安全知识库 */
export function getKnowledgeBase() {
  return request.get<KnowledgeBaseResponse>('/api/security/knowledge-base')
}
