import {request} from "@/api/request";


export interface RecordData {
  key: string
  value: number
}

export interface CourseDepData {
  chapters: Chapters[]
  course: Curriculum
  hours: Hours
  duration: number
  /** 是否必修 */
  is_required: boolean
  learn_hour_records: LearnHourRecords[]
  learn_record: LearnRecord | null
}

export interface HourPlayData {
  /** 时间 */
  duration: number
  /** 格式 */
  extension: string
  /** 地址 */
  url: string
  /** 断点题 */
  hourExamQuestions: Record<number, (ShareSubject | Multi)[]>
  /** 断点 */
  timeList: number[]
  hour_test?: { id: number }
}

export interface Course {
  audit_mode: boolean
  /** 完成 */
  finished_count: number;
  /** 未完成 */
  not_finished_count: number
  /** 选修课时数量 */
  not_required_count: number
  /** 必修课时数量 */
  required_count: number
  /** 今天 */
  today_learn_time: number
  /** 累计 */
  total_learn_time: number
}

interface AnswerRecord {
  question_id: number
  question_type: number
  is_pass: boolean
  user_id: number
  time: number
}

interface RecordTextParam {
  is_pass: boolean;
  test_id: number;
  /**  true true false 拼成的string */
  test_info: string;
  test_score: number;
  user_id: number;
}

export interface CurEndParam {
  duration: number
  unique_ident: number
  /** 开始时间时间戳 */
  start_date: number
}

export const curriculum = {
  /** 部门all */
  department() {
    return request<{ data: Manage[] }>('/api/v1/department/index', 'GET')
  },
  /** 学习记录 */
  record(id: number) {
    return request<RecordData[]>(`/api/v1/user/record/${id}`, "GET")
  },
  /** 查看课程课时数据 */
  courseDep(id: number, depId: number | null) {
    return request<CourseDepData>(`/api/v1/course/dep`, "GET", {id, depId})
  },
  /** 播放 */
  hourPlay(courseId: number, id: number) {
    return request<HourPlayData>(`/api/v1/course/${courseId}/hour/${id}/play`, "GET")
  },
  /** 删除课程 */
  delCur(dep_id: number, course_id: number) {
    return request(`/api/v1/department/assign/${dep_id}?course_id=${course_id}`, "DELETE")
  },
  /** 课程进度 */
  course() {
    return request<Course>(`/api/v1/user/all/course/info`, "GET")
  },
  /** 课程结束 */
  curEnd(courseId: number, id: number, data: CurEndParam) {
    return request(`/api/v1/course/${courseId}/hour/${id}/record`, "POST", data)
  },
  answerRecord(hourId: number, data: AnswerRecord) {
    return request<{ time: number }>(`/api/v1/test/video/record/${hourId}`, "POST", data)
  },
  /** 获取考卷 */
  getText(testId: string) {
    return request<Record<string, Multi[]>>(`/api/v1/test/bank/create/${testId}`, "GET")
  },
  /** 考卷记录 */
  recordText(hourId: number, data: RecordTextParam) {
    return request(`/api/v1/test/record/${hourId}`, 'POST', data)
  },
  putRecordText(hour_id: number, is_pass: boolean) {
    return request(`/api/v1/test/record/${hour_id}`, "PUT", {is_pass})
  },
  /** 学习记录 */
  hourCache(courseId: number, data: HourCacheParam) {
    return request<number>(`/api/v1/course/${courseId}/hour/cache`, "PUT", data)
  },
  commit(courseId: number, data: HourCacheParam) {
    return request(`/api/v1/course/${courseId}/hour/commit`, "PUT", data)
  }
}
