import axios from 'axios'

const api = axios.create({
  baseURL: '/api',
  timeout: 300000  // 5分钟超时，批量处理可能需要较长时间
})

/**
 * 文件对
 */
export interface FilePair {
  json_path: string
  image_path: string
}

/**
 * 批量处理请求
 */
export interface BatchProcessRequest {
  template_name: string
  file_pairs: FilePair[]
  output_dir: string
  parallel?: boolean
  adjust_rows?: boolean
}

/**
 * 批量处理结果
 */
export interface BatchProcessResult {
  success: boolean
  json_path: string
  image_path: string
  structure_path?: string
  filename: string
  rows?: number
  cols?: number
  error?: string
}

/**
 * 批量处理响应
 */
export interface BatchProcessResponse {
  success: boolean
  total: number
  processed: number
  failed: number
  results: BatchProcessResult[]
  message?: string
}

/**
 * 批量绘图请求
 */
export interface DrawBatchRequest {
  results: BatchProcessResult[]
  line_width?: number
  line_color?: number[]
}

/**
 * 批量绘图响应
 */
export interface DrawBatchResponse {
  success: boolean
  total: number
  drawn: number
  results: Array<{
    success: boolean
    image_path?: string
    filename: string
    error?: string
  }>
  message?: string
}

/**
 * 批量处理文件
 */
export async function batchProcess(request: BatchProcessRequest): Promise<BatchProcessResponse> {
  const response = await api.post<BatchProcessResponse>('/batch/process', request)
  return response.data
}

/**
 * 批量绘制表格线
 */
export async function batchDraw(request: DrawBatchRequest): Promise<DrawBatchResponse> {
  const response = await api.post<DrawBatchResponse>('/batch/draw', request)
  return response.data
}

/**
 * 健康检查
 */
export async function healthCheck(): Promise<{ status: string; service: string }> {
  const response = await api.get('/batch/health')
  return response.data
}
