import request from '@/utils/request'
import { AxiosPromise } from 'axios'
import { EvaluationQuery, EvaluationVO, EvaluationForm, ThirdPartyExam, SyncParams, EvaluationApplyRecordVO } from './types'

/**
 * 查询测评列表
 */
export const listEvaluation = (query: EvaluationQuery): AxiosPromise<EvaluationVO[]> => {
  return request({
    url: '/main/examEvaluation/list',
    method: 'get',
    params: query
  })
}

/**
 * 查询学生测评记录列表
 * @param studentId 学生ID
 * @returns 测评记录列表
 */
export const listEvaluationRecord = (studentId: string | number): AxiosPromise<EvaluationApplyRecordVO[]> => {
  return request({
    url: '/main/exam-apply/record/list',
    method: 'get',
    params: { studentId }
  })
}

/**
 * 查询测评详情
 * @param id 测评ID
 * @returns 测评详情数据
 */
export const getEvaluation = (id: string | number): AxiosPromise<EvaluationVO> => {
  return request({
    url: `/main/examEvaluation/${id}`,
    method: 'get'
  })
}

/**
 * 新增测评
 * @param data 测评信息
 * @returns 新增结果
 */
export const addEvaluation = (data: EvaluationForm): AxiosPromise<number | string> => {
  return request({
    url: '/main/examEvaluation',
    method: 'post',
    data: data
  })
}

/**
 * 修改测评
 * @param data 测评信息
 * @returns 修改结果
 */
export const updateEvaluation = (data: EvaluationForm): AxiosPromise<void> => {
  return request({
    url: '/main/examEvaluation',
    method: 'put',
    data: data
  })
}

/**
 * 删除测评
 * @param ids 测评ID数组
 * @returns 删除结果
 */
export const delEvaluation = (ids: string | number | (string | number)[]): AxiosPromise<void> => {
  return request({
    url: `/main/examEvaluation/${ids}`,
    method: 'delete'
  })
}

/**
 * 导出测评数据
 * @param query 查询参数
 * @returns 导出结果
 */
export const exportEvaluation = (query: EvaluationQuery): AxiosPromise<void> => {
  return request({
    url: '/main/examEvaluation/export',
    method: 'post',
    data: query
  })
}

/**
 * 批量删除测评
 * @param ids ID数组
 * @returns 操作结果
 */
export const batchDeleteEvaluation = (ids: (string | number)[]): AxiosPromise<void> => {
  return delEvaluation(ids.join(','))
}

/**
 * 批量上架测评
 * @param ids ID数组
 * @returns 操作结果
 */
export const batchOnlineEvaluation = (ids: (string | number)[]): AxiosPromise<void> => {
  return request({
    url: '/main/examEvaluation/updateStatus',
    method: 'put',
    params: { status: '1' },
    data: ids
  })
}

/**
 * 批量下架测评
 * @param ids ID数组
 * @returns 操作结果
 */
export const batchOfflineEvaluation = (ids: (string | number)[]): AxiosPromise<void> => {
  return request({
    url: '/main/examEvaluation/updateStatus',
    method: 'put',
    params: { status: '2' },
    data: ids
  })
}

/**
 * 更新状态
 * @param ids ID数组
 * @param status 状态
 * @returns 操作结果
 */
export const updateStatus = (ids: (string | number)[], status: string): AxiosPromise<void> => {
  return request({
    url: '/main/examEvaluation/updateStatus',
    method: 'put',
    params: { status },
    data: ids
  })
}

/**
 * 获取第三方考试列表
 * @param page 页码
 * @returns 第三方考试列表
 */
export const getThirdPartyExamList = (page: number = 1): AxiosPromise<string> => {
  return request({
    url: '/main/examEvaluation/exam-list',
    method: 'post',
    data: {
      page: page,
      examStartTime: "2026-01-01",
      examEndTime: "2026-12-31",
      examIds: "",
      expSeconds: 300
    }
  })
}

/**
 * 获取第三方考试详情
 * @param examId 考试ID
 * @returns 考试详情
 */
export const getThirdPartyExamDetail = (examId: string | number): AxiosPromise<ThirdPartyExam> => {
  return request({
    url: `/main/third-party/exam/detail/${examId}`,
    method: 'get'
  })
}

/**
 * 同步测评至单位
 * @param id 测评ID
 * @param params 同步参数
 * @returns 同步结果
 */
export const syncToCompany = (id: string | number, params?: SyncParams): AxiosPromise<void> => {
  return request({
    url: `/main/evaluation/syncToCompany/${id}`,
    method: 'post',
    data: params
  })
}

/**
 * 同步测评至员工
 * @param id 测评ID
 * @param params 同步参数
 * @returns 同步结果
 */
export const syncToEmployee = (id: string | number, params?: SyncParams): AxiosPromise<void> => {
  return request({
    url: `/main/evaluation/syncToEmployee/${id}`,
    method: 'post',
    data: params
  })
}

/**
 * 刷新数据
 * @returns 刷新结果
 */
export const refreshData = (): AxiosPromise<void> => {
  return request({
    url: '/main/evaluation/refresh',
    method: 'get'
  })
}

/**
 * 获取学员测评详细考试结果（对接考试星10000接口）
 * @param evaluationId 测评ID
 * @param studentId 学员ID
 * @returns 测评详情结果（包含每个能力维度的多次作答记录）
 */
export const getEvaluationDetailResult = (evaluationId: string | number, studentId: string | number): AxiosPromise<any> => {
  return request({
    url: `/main/examEvaluation/detail-result/${evaluationId}`,
    method: 'get',
    params: { studentId }
  })
}
