/**
 * 用例 API 接口
 *
 * @module api/cases
 * @author czj
 * @date 2026-07-09
 */

import request from '@/utils/request'
import type { TestCase, CaseListResponse, CreateCaseRequest } from '@/types/case'

/**
 * 用例 API 服务
 */
export const caseApi = {
  /**
   * 获取用例列表
   */
  async list(params?: {
    module_id?: string
    keyword?: string
    status?: string
    priority?: string
    case_type?: string
    skip?: number
    limit?: number
  }): Promise<CaseListResponse> {
    const response = await request.get('/api/cases', { params })
    return response as any
  },

  /**
   * 获取单个用例详情
   */
  async get(id: string): Promise<TestCase> {
    const response = await request.get(`/api/cases/${id}`)
    return response as any
  },

  /**
   * 创建用例
   */
  async create(data: CreateCaseRequest): Promise<TestCase> {
    const response = await request.post('/api/cases', data)
    return response as any
  },

  /**
   * 更新用例
   */
  async update(id: string, data: Partial<CreateCaseRequest>): Promise<TestCase> {
    const response = await request.put(`/api/cases/${id}`, data)
    return response as any
  },

  /**
   * 删除用例
   */
  async delete(id: string): Promise<void> {
    await request.delete(`/api/cases/${id}`)
  },

  /**
   * 复制用例
   */
  async copy(id: string): Promise<TestCase> {
    const response = await request.post(`/api/cases/${id}/copy`)
    return response as any
  },

  /**
   * 导入用例
   */
  async import(data: any[]): Promise<{ success_count: number; failed_count: number; errors: string[] }> {
    const response = await request.post('/api/cases/import', data)
    return response as any
  },

  /**
   * 导出用例
   */
  async export(caseIds?: string[]): Promise<any[]> {
    const response = await request.post('/api/cases/export', caseIds)
    return response as any
  },
}