import { httpApp } from "@/plugins/axiosApp";
import {
  ExamQuestion,
  OnlineExam,
  OnlinePracticeRecord,
  OnlinePracticeRecordResult,
  PaperStruct,
  PracticeExam,
  QuestionUnitItem,
} from "@/types/student-client";

type ExamItem = {
  id: number;
  name: string;
  enable: boolean
};

/** 获取在线练习考试列表 */
export async function onlinePracticeExamListApi(studentId: number) {
  return httpApp.get<any, { data: ExamItem[] }>(
    `/api/ecs_exam_work/exam/queryByNameLike`,
    {
      params: {
        examTypes: "PRACTICE",
        name: "",
        studentId,
      },
    }
  );
}
/** 获取在线练习考试科目列表 */
export async function onlinePracticeCourseListApi(examId: number) {
  return httpApp.get<any, { data: PracticeExam[] }>(
    `/api/branch_ecs_oe_admin/practice/queryPracticeCourseList`,
    {
      params: {
        examId,
      },
    }
  );
}
/** 获取在线练习考试记录列表 */
export async function onlinePracticeRecordListApi(
  examStudentId: number | string
) {
  return httpApp.get<any, { data: OnlinePracticeRecord[] }>(
    `/api/branch_ecs_oe_admin/practice/queryPracticeRecordList`,
    {
      params: {
        examStudentId,
      },
    }
  );
}

/** 获取在线练习考试成绩报告 */
export async function onlinePracticeRecordDetailApi(
  examRecordDataId: number | string,
  fromCache?: boolean
) {
  const params: {
    examRecordDataId: string | number;
    fromCache?: string;
  } = {
    examRecordDataId,
  };
  if (fromCache) params.fromCache = "1";
  return httpApp.get<any, { data: OnlinePracticeRecordResult }>(
    `/api/branch_ecs_oe_admin/practice/getPracticeDetailInfo`,
    {
      params,
    }
  );
}

// 试卷预览 ---------->
/** 获取试卷练习类型 */
export async function onlinePracticeTypeApi(examId: number | string) {
  return httpApp.get<any, { data: { PRACTICE_TYPE: string } }>(
    `/api/ecs_exam_work/exam/getExamPropertyFromCacheByStudentSession/${examId}/PRACTICE_TYPE`
  );
}
/** 获取考试数据 */
export async function onlineExamDataApi(examId: number | string) {
  return httpApp.get<any, { data: OnlineExam }>(
    `/api/ecs_exam_work/exam/${examId}`
  );
}
/** 获取考试记录试卷结构数据 */
export async function examRecordPaperStructApi(
  examRecordDataId: number | string,
  fromCache?: boolean
) {
  const params: {
    examRecordDataId: string | number;
    fromCache?: string;
  } = {
    examRecordDataId,
  };
  if (fromCache) params.fromCache = "1";

  return httpApp.get<any, { data: PaperStruct }>(
    `/api/branch_ecs_oe_admin/examRecordPaperStruct/getExamRecordPaperStruct`,
    {
      params,
    }
  );
}
/** 获取考试记录试题数据 */
export async function examRecordQuestionsApi(
  examRecordDataId: number | string,
  fromCache?: boolean
) {
  const params: {
    examRecordDataId: string | number;
    fromCache?: string;
  } = {
    examRecordDataId,
  };
  if (fromCache) params.fromCache = "1";

  return httpApp.get<any, { data: { examQuestionEntities: ExamQuestion[] } }>(
    `/api/branch_ecs_oe_admin/examRecordQuestions/getExamRecordQuestions`,
    {
      params,
    }
  );
}
/** 获取考试记录数据 */
export async function examRecordDataApi(
  examRecordDataId: number | string,
  fromCache?: boolean
) {
  const params: {
    examRecordDataId: string | number;
    fromCache?: string;
  } = {
    examRecordDataId,
  };
  if (fromCache) params.fromCache = "1";

  return httpApp.get<any, { data: { examRecord: { paperType: string } } }>(
    `/api/branch_ecs_oe_admin/exam/record/data/findExamRecordDataEntity`,
    {
      params,
    }
  );
}

interface QuestionDataParam {
  questionId: string | number;
  examId: string | number;
  courseCode: string;
  groupCode: string;
}
export interface QuestionData {
  id: string;
  masterVersion: {
    body: string;
    questionUnitList: QuestionUnitItem[];
  };
}
/** 获取试题内容 */
export async function examQuestionDataApi(datas: QuestionDataParam) {
  return httpApp.post<any, { data: QuestionData }>(
    `/api/branch_ecs_ques/default_question/question`,
    datas
  );
}
