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

export type GetSubjectTextBookVersionApi = {
  subjectId: any
  gradeId?: any
  stageId?: any
  size?: any
  resource: any
}

interface Video {
  // 根据实际视频数据结构补充
  [key: string]: any
}

interface Knowledge {
  id: any
  baseId: any
  pid: any
  pids: string
  name: string
  status: number
  createTime: string
  childrenList: any[] // 可根据实际数据结构进一步定义
  children: any[] | null
  hasVideoFlag: number | null
  videoList: Video[] | null
}

interface ChapterVideo {
  // 根据实际章节视频数据结构补充
  [key: string]: any
}

export interface Chapter {
  id: any
  subjectId: number
  pid: any
  pids: string
  name: string
  sort: number
  learnFlag: number
  hasVideoFlag: number
  chapterVideoVoList: ChapterVideo[] | null
  children: Chapter[] // 递归类型，表示子章节
  knowledgeVoList: Knowledge[]
  correctCount: any
  maxCorrectCount: any
  lastCorrectCount: any

  progressText: any
  progressClass: any
}

interface SubjectChapterTreeParams {
  textBookId: any
  [key: string]: any
}

interface UserTextBookListParams {
  subjectId?: any
  gradeId?: any
  size?: number
  [key: string]: any
}

interface SaveUserTextBookData {
  textBookId: any
  subjectId: any
  gradeId: any
  [key: string]: any
}

interface UpdateUserTextBookData {
  id: any
  [key: string]: any
}

interface SubjectKnowledgeTreeParams {
  chapterId: any
  [key: string]: any
}

interface SubjectChapterVideosParams {
  chapterId: any
  [key: string]: any
}

interface SubjectKnowledgeVideosParams {
  knowledgeId: any
  [key: string]: any
}
export interface Color {
  color: string
  bgColor: string
}

export interface Clean {
  name: string
  tags: string[]
  all_tags: string[]
  fullName?: string
  color: Color
}

export interface TextBook {
  id: any
  textBookId: any
  baseId: any
  gradeId: any
  subjectId: any
  bookVersionId: any
  name: string
  shortName: string
  textBookName: string
  displayName: string
  businessFlag: string
  addFlag: number
  progress: any
  bookVersionName: any
  versionName: any
  currentChapterId: any
  chapterBaseId: any
  chapterTreeNodeList: any
  clean: Clean
  locked: boolean
  syncFlag: number
  version: BookVersion
  bookType: number
  currentChapterIds: any
}

// 学校教材
export const book_type_school = 1
// 其他教材
export const book_type_other = 2
export const book_sync_locked = 1
export const book_sync_unlocked = 0

export interface BookVersion {
  id: any
  baseId: any
  name: string
  shortName: string
  delFlag: number
  textBookVoList: TextBook[]
  clean: Clean
}

export interface PracticeModule {
  /**
   * 主键
   */
  id: number

  /**
   * 学校id
   */
  schoolId: number

  /**
   * 排序，由小到大
   */
  sort: number

  /**
   * 模块名称
   */
  name: string

  /**
   * 状态（字典 0正常 1停用）
   */
  status: 0 | 1

  /**
   * 删除状态（0正常，1已删除）
   */
  delFlag: 0 | 1
}

export const subjectApi = {
  // 获取教材出版社
  getSubjectTextBookVersion: async (params: GetSubjectTextBookVersionApi) => {
    const res = (await get('/subjectTextBookVersion/list', {
      size: -1,
      ...params,
    })) as any
    if (Array.isArray(res?.rows)) return res.rows
    if (Array.isArray(res?.data?.rows)) return res.data.rows
    if (Array.isArray(res)) return res
    return []
  },

  // 获取章节
  getSubjectChapterTree: (params: SubjectChapterTreeParams) => {
    return http.request({
      url: '/subjectChapter/tree',
      params,
    })
  },

  // 获取用户教材
  getUserTextBookList: async (params: UserTextBookListParams) => {
    if (!params.size) {
      params.size = -1
    }
    return await get<any>('/userTextBook/list', params)
  },

  // 添加用户教材
  saveUserTextBook: (data: SaveUserTextBookData) => {
    return http.request({
      url: '/userTextBook',
      method: 'post',
      data,
    })
  },

  // 更新用户教材
  updateUserTextBook: (data: UpdateUserTextBookData) => {
    return http.request({
      url: '/userTextBook',
      method: 'put',
      data,
    })
  },

  // 删除用户教材
  delUserTextBookToId: (id: any) => {
    return http.request({
      url: `/userTextBook/${id}`,
      method: 'delete',
    })
  },

  // 学科知识点-查询章节下的知识点树
  getSubjectKnowledgeTree: (params: SubjectKnowledgeTreeParams) => {
    return http.request({
      url: `/subjectKnowledge/tree`,
      params,
    })
  },

  // 学科章节-视频表-查询列表
  getSubjectChapterVideos: (params: SubjectChapterVideosParams) =>
    http.request({
      url: '/subjectChapterVideo/list',
      params,
    }),

  // 学科知识点-视频表-查询列表
  getSubjectKnowledgeVideos: (params: SubjectKnowledgeVideosParams) =>
    http.request({
      url: '/subjectKnowledgeVideo/list',
      params,
    }),

  // 用户教材表-修改当前所处章节
  updateCurrentChapter: (data: anyObj) =>
    http.request({
      url: '/userTextBook/updateCurrentChapter',
      method: 'post',
      data,
    }),

  /**
   * 教材查询
   * @param id
   * @returns
   */
  getTextBookById: async (id: string) => {
    return await get<TextBook>(`/userTextBook/${id}`)
  },

  /**
   *
   * @param params
   * @returns
   */
  getPracticeModuleList: async (params: any) => {
    return await get<PageResponse<PracticeModule>>('/school/practiceModule/list', params)
  },

  /*
   ** 错题本
   */
  // 用户错题本表-查询列表
  getWrongQuesList: (params: anyObj) =>
    http.request({
      url: '/userErrorEdition/list',
      params,
    }),

  // 用户错题本表-查询列表
  getUserErrorInfo: (params: anyObj) =>
    http.request({
      url: '/userErrorEdition/getUserErrorInfo',
      params,
    }),

  /*
   ** ai精准学
   */

  // 用户知识点表-id查询
  getSujectKnowledgeStatistics: (params: anyObj) =>
    http.request({
      url: '/userKnowledge/getSujectKnowledgeStatistics',
      params,
    }),

  // 用户组卷表-查询列表
  getGroupPaperList: (params: anyObj) =>
    http.request({
      url: '/userPaper/list',
      params,
    }),

  // 用户组卷表-id查询
  getGroupPaperDetail: (id: any) =>
    http.request({
      url: '/userPaper/' + id,
    }),
}

export default subjectApi
