/**
 * 执行管理 API 接口
 *
 * @module api/executions
 * @author czj
 * @date 2026-07-09
 */

import request from '@/utils/request'

/**
 * 执行管理 API 服务
 */
export const executionApi = {
  /**
   * 获取执行记录列表
   */
  async list(params?: {
    skip?: number
    limit?: number
    status?: string
    case_type?: string
  }): Promise<{ total: number; items: any[] }> {
    const response = await request.get('/api/executions', { params })
    return response as any
  },

  /**
   * 获取执行详情
   */
  async get(id: string): Promise<any> {
    const response = await request.get(`/api/executions/${id}`)
    return response as any
  },

  /**
   * 创建执行任务
   */
  async create(data: {
    case_ids: string[]
    name?: string
    environment?: string
    config?: Record<string, any>
  }): Promise<any> {
    const response = await request.post('/api/executions', data)
    return response as any
  },

  /**
   * 运行执行任务
   */
  async run(id: string, config?: Record<string, any>): Promise<any> {
    const response = await request.post(`/api/executions/${id}/run`, config)
    return response as any
  },

  /**
   * 取消执行任务
   */
  async cancel(id: string): Promise<any> {
    const response = await request.post(`/api/executions/${id}/cancel`)
    return response as any
  },

  /**
   * 获取执行结果列表
   */
  async getResults(id: string): Promise<{ total: number; items: any[] }> {
    const response = await request.get(`/api/executions/${id}/results`)
    return response as any
  },

  /**
   * 获取单个用例结果
   */
  async getCaseResult(executionId: string, caseId: string): Promise<any> {
    const response = await request.get(
      `/api/executions/${executionId}/results/${caseId}`
    )
    return response as any
  },

  /**
   * 删除执行记录（不影响测试用例本身）
   */
  async delete(id: string): Promise<void> {
    await request.delete(`/api/executions/${id}`)
  },

  /**
   * 批量删除执行记录
   * 运行中的记录会被自动跳过
   */
  async batchDelete(ids: string[]): Promise<{
    success_count: number
    failed_count: number
    skipped_count: number
    errors: string[]
  }> {
    const response = await request.post('/api/executions/batch-delete', { ids })
    return response as any
  },
}