/**
 * 管理员 - 学员考试成绩API
 */
import request from '@/api/request'

/**
 * 学员考试成绩查询参数
 */
export interface StudentScoresParams {
  page: number
  size: number
  student_name?: string
  position?: string
  course_id?: number
  score_range?: string  // excellent/good/pass/fail
  start_date?: string
  end_date?: string
}

/**
 * 学员考试成绩记录
 */
export interface StudentScoreRecord {
  id: number
  student_id: number
  student_name: string
  position: string | null
  course_id: number
  course_name: string
  exam_type: string
  score: number
  round1_score: number | null
  round2_score: number | null
  round3_score: number | null
  total_score: number
  accuracy: number | null
  correct_count: number | null
  wrong_count: number
  total_count: number
  duration_seconds: number | null
  exam_date: string | null
}

/**
 * 错题记录
 */
export interface MistakeRecord {
  id: number
  question_content: string
  correct_answer: string
  user_answer: string
  question_type: string
  analysis: string
  created_at: string | null
}

/**
 * 考试成绩统计数据
 */
export interface ScoresStatistics {
  total_exams: number
  avg_score: number
  pass_rate: number
  excellent_rate: number
}

/**
 * 获取学员考试成绩列表
 */
export function getStudentScores(params: StudentScoresParams) {
  return request.get<{
    items: StudentScoreRecord[]
    total: number
    page: number
    page_size: number
    pages: number
  }>('/api/v1/manager/student-scores', { params })
}

/**
 * 获取学员考试成绩统计
 */
export function getStudentScoresStatistics() {
  return request.get<ScoresStatistics>('/api/v1/manager/student-scores/statistics')
}

/**
 * 获取考试的错题记录
 */
export function getExamMistakes(examId: number) {
  return request.get<{
    items: MistakeRecord[]
    total: number
    page: number
    page_size: number
    pages: number
  }>(`/api/v1/manager/student-scores/${examId}/mistakes`)
}

/**
 * 删除单条考试记录
 */
export function deleteExamRecord(examId: number) {
  return request.delete<{ deleted_id: number }>(`/api/v1/manager/student-scores/${examId}`)
}

/**
 * 批量删除考试记录
 */
export function batchDeleteExamRecords(ids: number[]) {
  return request.delete<{ deleted_count: number; deleted_ids: number[] }>(
    '/api/v1/manager/student-scores/batch/delete',
    { data: { ids } }
  )
}

