/**
 * 成绩查询相关API
 */
import request from './request'

/**
 * 考试记录列表参数
 */
export interface ExamRecordsParams {
  page: number
  size: number
  course_id?: number
  start_date?: string
  end_date?: string
}

/**
 * 题型统计
 */
export interface QuestionTypeStat {
  type: string
  type_code: string
  total: number
  correct: number
  wrong: number
  accuracy: number
  total_score: number
}

/**
 * 考试记录
 */
export interface ExamRecord {
  id: number
  course_id: number
  course_name: string
  exam_name: string
  question_count: number
  total_score: number
  score: number | null
  is_passed: boolean | null
  status: string
  start_time: string | null
  end_time: string | null
  created_at: string
  // 统计字段
  accuracy: number | null
  correct_count: number | null
  wrong_count: number | null
  duration_seconds: number | null
  question_type_stats: QuestionTypeStat[]
}

/**
 * 考试记录列表响应
 */
export interface ExamRecordsResponse {
  items: ExamRecord[]
  total: number
  page: number
  size: number
  pages: number
}

/**
 * 考试统计概览
 */
export interface ExamStatistics {
  total_exams: number
  avg_score: number
  pass_rate: number
  total_questions: number
}

/**
 * 考试详情
 */
export interface ExamDetail {
  id: number
  course_id: number
  exam_name: string
  question_count: number
  total_score: number
  pass_score: number
  start_time: string | null
  end_time: string | null
  duration_minutes: number
  status: string
  score: number | null
  is_passed: boolean | null
  questions: any
  answers: any
}

/**
 * 获取考试记录列表
 */
export function getExamRecords(params: ExamRecordsParams) {
  return request.get<ExamRecordsResponse>('/api/v1/exams/records', params)
}

/**
 * 获取考试统计概览
 */
export function getExamStatistics(params?: { course_id?: number }) {
  return request.get<ExamStatistics>('/api/v1/exams/statistics/summary', params)
}

/**
 * 获取考试详情
 */
export function getExamDetail(examId: number) {
  return request.get<ExamDetail>(`/api/v1/exams/${examId}`)
}

/**
 * 获取课程列表（用于筛选下拉框）
 */
export function getCourseList() {
  return request.get('/api/v1/courses', {
    page: 1,
    size: 100,
    status: 'published'
  })
}

