/**
 * 安全测试 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): Promise<{ execution_id: string; content: string; format: string }> {
  return request.get(`/api/security/executions/${executionId}/report`) as any
}

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

// ==================== ERP 上传 ====================

/** 上传安全测试报告到 ERP 测试单 */
export function uploadSecurityReportToErp(
  executionId: string,
  data: {
    developtesting_id?: number
    copyuser_names?: string[]
    createuser_name?: string
  }
): Promise<{ success: boolean; report_id?: number; message: string }> {
  return request.post(`/api/security/executions/${executionId}/upload-to-erp`, data) as any
}

/** 上传安全测试报告到 ERP 项目资料 */
export function uploadSecurityReportToProject(
  executionId: string,
  data: {
    project_id: number
    name?: string
  }
): Promise<{ success: boolean; file_id?: number; message: string }> {
  return request.post(`/api/security/executions/${executionId}/upload-to-project`, data) as any
}

/** 上传安全测试报告到 ERP 协作文档 */
export function uploadSecurityReportToCooperation(
  executionId: string,
  data: {
    project_id: number
    name?: string
    group_id?: number
  }
): Promise<{ success: boolean; file_id?: number; message: string }> {
  return request.post(`/api/security/executions/${executionId}/upload-to-cooperation`, data) as any
}

// ==================== ERP 任务创建 ====================

/** 获取安全测试 ERP 任务创建预览数据 */
export function getSecurityTaskPreview(executionId: string): Promise<any> {
  return request.get(`/api/security/executions/${executionId}/task-preview`) as any
}

/** 创建安全测试 ERP 任务 */
export function createSecurityTask(
  executionId: string,
  data: {
    type_id: number
    status_id: number
    level: number
    excepthour: number
    user_id: number
    createuser_id: number
    deadline_days: number
    develop_id?: number
    task_name?: string
  }
): Promise<{ success: boolean; task_id?: number; task_name: string; message: string }> {
  return request.post(`/api/security/executions/${executionId}/create-task`, data) as any
}

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

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