/**
 * 功能测试报告 API 接口
 *
 * @module api/functionalReport
 * @author czj
 * @date 2026-08-12
 */

import request from '@/utils/request'

/**
 * 功能测试报告 API 服务
 */
export const functionalReportApi = {
  /**
   * 上传测试用例和 BUG 列表 Excel
   * @param testcaseFile 测试用例 Excel 文件
   * @param buglistFile BUG 列表 Excel 文件
   * @param sessionId 会话 ID（可选）
   */
  async upload(
    testcaseFile: File,
    buglistFile: File,
    sessionId: string = ''
  ): Promise<{ session_id: string; testcase_path: string; buglist_path: string; message: string }> {
    const formData = new FormData()
    formData.append('testcase_file', testcaseFile)
    formData.append('buglist_file', buglistFile)
    formData.append('session_id', sessionId)
    const response = await request.post('/api/functional-report/upload', formData, {
      headers: { 'Content-Type': 'multipart/form-data' }
    })
    return response as any
  },

  /**
   * 生成功能测试报告
   * @param sessionId 会话 ID
   * @param projectName 项目名称
   * @param reportFormat 报告格式: docx / md / both
   * @param erpOptions ERP 自动上传选项
   */
  async generate(
    sessionId: string,
    projectName: string,
    reportFormat: string = 'docx',
    erpOptions?: {
      upload_to_erp?: boolean
      upload_to_project?: boolean
      upload_to_cooperation?: boolean
      developtesting_id?: number
      copyuser_names?: string[]
      createuser_name?: string
      project_id?: number
      project_file_name?: string
      cooperation_project_id?: number
      cooperation_file_name?: string
      cooperation_group_id?: number
    }
  ): Promise<{
    session_id: string
    project_name: string
    case_analysis: any
    bug_analysis: any
    case_bug_link_count: number
    charts: Record<string, string>
    reports: string[]
    erp_results: { type: string; label: string; success: boolean; message: string }[]
  }> {
    const response = await request.post('/api/functional-report/generate', {
      session_id: sessionId,
      project_name: projectName,
      report_format: reportFormat,
      ...(erpOptions || {})
    })
    return response as any
  },

  /**
   * 获取会话报告信息
   * @param sessionId 会话 ID
   */
  async info(sessionId: string): Promise<{
    session_id: string
    reports: { name: string; path: string; size: number }[]
    charts: { name: string; path: string }[]
    analysis: any
  }> {
    const response = await request.get(`/api/functional-report/info/${sessionId}`)
    return response as any
  },

  /**
   * 下载报告文件地址
   * @param sessionId 会话 ID
   * @param filename 报告文件名（可选）
   */
  downloadUrl(sessionId: string, filename: string = ''): string {
    const query = filename ? `?filename=${encodeURIComponent(filename)}` : ''
    return `/api/functional-report/download/${sessionId}${query}`
  },

  /**
   * 预览 Markdown 报告地址
   * @param sessionId 会话 ID
   */
  previewUrl(sessionId: string): string {
    return `/api/functional-report/preview/${sessionId}`
  },

  /**
   * 上传报告到 ERP
   * @param sessionId 会话 ID
   * @param reportFilename 报告文件名
   * @param developtestingId 测试单 ID（可选）
   * @param copyuserNames 抄送人姓名列表
   * @param createuserName 创建人姓名（可选）
   */
  async uploadToErp(
    sessionId: string,
    reportFilename: string,
    developtestingId?: number,
    copyuserNames: string[] = [],
    createuserName?: string
  ): Promise<{ success: boolean; report_id?: number; message: string }> {
    const response = await request.post('/api/functional-report/upload-to-erp', {
      session_id: sessionId,
      report_filename: reportFilename,
      developtesting_id: developtestingId,
      copyuser_names: copyuserNames,
      createuser_name: createuserName
    })
    return response as any
  },

  /**
   * 获取 ERP 配置
   */
  async getErpConfig(): Promise<{
    api_key: string
    base_url: string
    developtesting_id: number
    type_id: number
    image_url_prefix: string
  }> {
    const response = await request.get('/api/functional-report/config')
    return response as any
  },

  /**
   * 更新 ERP 配置
   */
  async updateErpConfig(config: {
    api_key?: string
    base_url?: string
    developtesting_id?: number
    type_id?: number
    image_url_prefix?: string
  }): Promise<any> {
    const response = await request.put('/api/functional-report/config', config)
    return response as any
  },

  /**
   * 上传文件到 ERP 项目资料
   * @param sessionId 会话 ID
   * @param reportFilename 报告文件名
   * @param projectId 关联的项目 ID
   * @param name 文件名称（可选）
   */
  async uploadToProject(
    sessionId: string,
    reportFilename: string,
    projectId: number,
    name?: string
  ): Promise<{ success: boolean; file_id?: number; message: string }> {
    const response = await request.post('/api/functional-report/upload-to-project', {
      session_id: sessionId,
      report_filename: reportFilename,
      project_id: projectId,
      name: name
    })
    return response as any
  },

  /**
   * 上传文件到 ERP 协作文档
   * @param sessionId 会话 ID
   * @param reportFilename 报告文件名
   * @param projectId 关联的项目 ID
   * @param name 文件名称（可选）
   * @param groupId 分组 ID（可选）
   */
  async uploadToCooperation(
    sessionId: string,
    reportFilename: string,
    projectId: number,
    name?: string,
    groupId?: number
  ): Promise<{ success: boolean; file_id?: number; message: string }> {
    const response = await request.post('/api/functional-report/upload-to-cooperation', {
      session_id: sessionId,
      report_filename: reportFilename,
      project_id: projectId,
      name: name,
      group_id: groupId
    })
    return response as any
  }
}