import { get, post } from '../request'
import type { PageResponse } from '../request'

export interface QuestionTitle {
  actualScore: number
  aiAnalyze: string
  aiAnswer: string
  answer: string
  answerHtml: string
  answerSqs: string
  correctResult: number
  courseId: number
  delFlag: number
  difficulty: number
  difficultyCode: number
  difficultyLevel: string
  explanation: string
  id: string
  itemId: number
  media: number
  optionHtml: string
  optionSqs: string
  point: string
  schoolId: number
  score: number
  sort: number
  status: number
  stem: string
  stemHtml: string
  stemSqs: string
  stemType: number
  submitAnswer: string
  submitAnswerPic: string
  textBookId: number
  typeId: string
  typeName: string
  year: number
}

export interface WrongQuestionItem {
  id: number
  questionId: string
  userId: number
  subjectId: number
  textbookId: number
  chapterId: number
  courseId: number
  errorCount: number
  errorType: number
  masteryLevel: number
  status: number
  delFlag: number
  firstErrorTime: string
  lastErrorTime: string
  lastCorrectTime: string
  questionTitle: QuestionTitle
}

export interface WrongQuestionParams {
  current: number
  size: number
  chapterId?: number
  courseId?: number
  startTime?: string
  endTime?: string
  subjectId?: number
}

export interface RetryStartParams {
  chapterId?: number
  courseId?: number
  questionCount?: number
  subjectId?: number
  textbookId?: number
}

export interface RetryStartResult {
  costTime: number
  paperId: number
  paperName: string
  recordId: number
  remark: string
  titleVoList: QuestionTitle[]
}

export interface FilterSubject {
  id: number
  name: string
}

export interface FilterTextbook {
  id: number
  name: string
}

export interface FilterChapter {
  id: number
  name: string
  parentId: number
  textbookId: number
  type: string
}

const wrongQuestionApi = {
  getList: async (params: WrongQuestionParams): Promise<PageResponse<WrongQuestionItem>> => {
    return await get<PageResponse<WrongQuestionItem>>('/xkw/wrong/question/filter/page', params)
  },
  retryStart: async (params: RetryStartParams): Promise<RetryStartResult> => {
    return await post<RetryStartResult>('/xkw/wrong/question/retry/start', params)
  },
  similarRecommend: async (params: { questionId: string }): Promise<RetryStartResult> => {
    return await post<RetryStartResult>('/school/paper/similarPractice/start', params)
  },
  getFilterSubjects: async (): Promise<FilterSubject[]> => {
    return await get<FilterSubject[]>('/xkw/wrong/question/filter/subjects')
  },
  getFilterTextbooks: async (params: { subjectId: number }): Promise<FilterTextbook[]> => {
    return await get<FilterTextbook[]>('/xkw/wrong/question/filter/textbooks', params)
  },
  getTextbookCatalogList: async (params: {
    textbookId: number
    size?: number
  }): Promise<FilterChapter[]> => {
    const res = await get<any>('/xkw/textbookCatalog/list', {
      textbookId: params.textbookId,
      size: params.size ?? -1,
    })
    if (Array.isArray(res)) return res
    if (res && typeof res === 'object') {
      return res.rows || res.records || res.list || []
    }
    return []
  },
}

export default wrongQuestionApi
