import { httpApp } from "@/plugins/axiosApp";
import { CommonResponse, HistoryQueryParams, Question, Task } from "@/types";

/** 清理复核任务 */
export async function clearInspectedTaskOfLibraryInspect(
  subjectCode?: string,
  groupNumber?: string
) {
  const form = new FormData();
  subjectCode && form.append("subjectCode", subjectCode);
  groupNumber && form.append("groupNumber", groupNumber);
  return httpApp.post<void>("/api/admin/exam/library/clear", form);
}

/** 批量复核历史 */
export async function getLibraryInspectedHistoryOfLibraryInspect({
  subjectCode,
  order = "markerTime",
  sort = "DESC",
  pageNumber = 1,
  pageSize = 20,
  groupNumber = "",
  secretNumber,
}: HistoryQueryParams) {
  if (!subjectCode) return;
  const form = new FormData();
  secretNumber && form.append("secretNumber", secretNumber);
  form.append("subjectCode", subjectCode);
  form.append("order", order);
  form.append("sort", sort);
  form.append("pageNumber", pageNumber + "");
  form.append("pageSize", pageSize + "");
  groupNumber && form.append("groupNumber", groupNumber);
  return httpApp.post<Task[]>("/api/admin/exam/library/getHistory", form);
}

/** 批量复核得到单个学生的复核任务 */
export async function getOneOfInspectedTaskOfLibraryInspect({
  subjectCode,
  groupNumber,
  markerId,
  examNumber,
  secretNumber,
  startScore,
  endScore,
  questionScore,
  unselective,
}: {
  subjectCode?: string;
  groupNumber?: string;
  markerId?: string;
  examNumber?: string;
  secretNumber?: string;
  startScore?: string;
  endScore?: string;
  questionScore?: string;
  unselective?: string;
}) {
  const form = new FormData();
  subjectCode && form.append("subjectCode", subjectCode);
  groupNumber && form.append("groupNumber", groupNumber);
  markerId && form.append("markerId", markerId);
  examNumber && form.append("examNumber", examNumber);
  secretNumber && form.append("secretNumber", secretNumber);
  startScore && form.append("startScore", startScore);
  endScore && form.append("endScore", endScore);
  questionScore && form.append("questionScore", questionScore);
  unselective && form.append("unselective", unselective);
  return httpApp.post<Task>("/api/admin/exam/library/getTask", form);
}

/** 批量复核得到任务总数 */
export async function getInspectedTaskStatusOfLibraryInspect({
  subjectCode,
  groupNumber,
  markerId,
  examNumber,
  secretNumber,
  startScore,
  endScore,
  questionScore,
  unselective,
}: {
  subjectCode: string;
  groupNumber: string;
  markerId?: string;
  examNumber?: string;
  secretNumber?: string;
  startScore?: string;
  endScore?: string;
  questionScore?: string;
  unselective?: string;
}) {
  const form = new FormData();
  form.append("subjectCode", subjectCode);
  form.append("groupNumber", groupNumber);
  markerId && form.append("markerId", markerId);
  examNumber && form.append("examNumber", examNumber);
  secretNumber && form.append("secretNumber", secretNumber);
  startScore && form.append("startScore", startScore);
  endScore && form.append("endScore", endScore);
  questionScore && form.append("questionScore", questionScore);
  unselective && form.append("unselective", unselective);
  return httpApp.post<{ valid: boolean; totalCount: number }>(
    "/api/admin/exam/library/getStatus",
    form
  );
}

/** 保存复核任务 */
export async function saveInspectedTaskOfLibraryInspect(taskId: string) {
  const form = new FormData();
  form.append("taskId", taskId);
  return httpApp.post<CommonResponse>(
    "/api/admin/exam/library/inspected/save",
    form
  );
}

/** 复核任务打回问题 */
export async function rejectInspectedTaskOfLibraryInspect(
  taskId: string,
  questionList: Array<Question>,
  reason: string
) {
  questionList = JSON.parse(
    JSON.stringify(questionList, (key, value) =>
      // eslint-disable-next-line @typescript-eslint/no-unsafe-return
      !key.startsWith("__") ? value : undefined
    )
  );
  return httpApp.post<CommonResponse>("/api/admin/exam/library/rejected", {
    taskId,
    questionList,
    reason,
  });
}
