/**
 * 考试相关API
 * 
 * 注意：本文件包含Dify考试工作流相关API
 * 同时重新导出exam/index.ts中的传统考试API，避免导入路径冲突
 */
import { http } from '@/utils/http'

// 重新导出传统考试API（来自exam/index.ts）
export {
  createExam,
  getExamDetail,
  startExam,
  submitExam,
  getExamResult,
  getUserExamRecords,
  startDynamicExam,
  submitDynamicAnswer,
  getDynamicExamSession,
  endDynamicExam,
  // 新增：成绩报告和错题本相关API
  getExamReport,
  getMistakesList,
  getMistakesStatistics,
  updateRoundScore,
  markMistakeMastered
} from './exam/index'

// 重新导出类型定义
export type {
  ExamReportResponse,
  MistakeListItem,
  MistakesStatisticsResponse
} from './exam/index'

// ==================== 类型定义 ====================

/**
 * 错题记录项
 */
export interface MistakeRecordItem {
  id?: number
  question_id?: number | null
  knowledge_point_id?: number | null
  question_content: string
  correct_answer: string
  user_answer: string
}

/**
 * 生成考试请求
 */
export interface GenerateExamRequest {
  course_id: number
  position_id?: number
  current_round?: number  // 当前轮次(1/2/3)
  exam_id?: number  // 已存在的exam_id(第2、3轮传入)
  mistake_records?: string  // 错题记录JSON字符串,第一轮不传此参数,第二三轮传入上一轮错题的JSON字符串
  single_choice_count?: number
  multiple_choice_count?: number
  true_false_count?: number
  fill_blank_count?: number
  essay_count?: number
  difficulty_level?: number
}

/**
 * 生成考试响应
 */
export interface GenerateExamResponse {
  result: string  // 试题JSON数组(字符串格式)
  workflow_run_id?: string
  task_id?: string
  exam_id: number  // 真实的考试ID
}

/**
 * 判断答案请求
 */
export interface JudgeAnswerRequest {
  question: string
  correct_answer: string
  user_answer: string
  analysis: string
}

/**
 * 判断答案响应
 */
export interface JudgeAnswerResponse {
  is_correct: boolean
  correct_answer: string
  feedback?: string
}

/**
 * 记录错题请求
 */
export interface RecordMistakeRequest {
  exam_id: number
  round: number  // 考试轮次(1/2/3)
  question_id?: number | null
  knowledge_point_id?: number | null
  question_content: string
  correct_answer: string
  user_answer: string
  question_type?: string  // 题型(single/multiple/judge/blank/essay)
}

/**
 * 记录错题响应
 */
export interface RecordMistakeResponse {
  id: number
  created_at: string
}

/**
 * 获取错题记录响应
 */
export interface GetMistakesResponse {
  mistakes: MistakeRecordItem[]
}

// ==================== API 接口 ====================

/**
 * 生成考试试题(调用Dify试题生成器工作流)
 */
export function generateExam(data: GenerateExamRequest) {
  return http.post<GenerateExamResponse>('/api/v1/exams/generate', data, {
    showLoading: false,  // 页面已有"试题动态生成中"提示，不需要全局loading
    timeout: 300000,  // 5分钟超时
  })
}

/**
 * 判断主观题答案(调用Dify答案判断工作流)
 */
export function judgeAnswer(data: JudgeAnswerRequest) {
  return http.post<JudgeAnswerResponse>('/api/v1/exams/judge-answer', data, {
    showLoading: false,
    timeout: 60000,  // 1分钟超时
  })
}

/**
 * 记录错题
 */
export function recordMistake(data: RecordMistakeRequest) {
  return http.post<RecordMistakeResponse>('/api/v1/exams/record-mistake', data, {
    showLoading: false,
  })
}

/**
 * 获取错题记录
 * @param examId 考试ID
 * @param round 指定轮次（可选），用于获取特定轮次的错题
 *              第2轮考试时传入round=1，获取第1轮的错题
 *              第3轮考试时传入round=2，获取第2轮的错题
 *              不传则获取该考试的所有错题
 */
export function getMistakes(examId: number, round?: number) {
  let url = `/api/v1/exams/mistakes?exam_id=${examId}`
  if (round !== undefined) {
    url += `&round=${round}`
  }
  return http.get<GetMistakesResponse>(url, {
    showLoading: false,
  })
}


