import request from '@/axios';

// ==================== 武术赛事裁判-项目关联管理接口 ====================

/**
 * 裁判-项目关联分页查询
 * @param {Number} current - 当前页,默认1
 * @param {Number} size - 每页条数,默认10
 * @param {Object} params - 查询参数
 * @param {Number} params.competitionId - 赛事ID
 * @param {Number} params.judgeId - 裁判ID（可选）
 * @param {Number} params.projectId - 项目ID（可选）
 * @param {String} params.role - 角色（可选）
 */
export const getJudgeProjectList = (current, size, params) => {
  return request({
    url: '/api/blade-martial/judge-project/list',
    method: 'get',
    params: {
      current,
      size,
      ...params
    }
  })
}

/**
 * 获取裁判-项目关联详情
 * @param {Number} id - 关联ID
 */
export const getJudgeProjectDetail = (id) => {
  return request({
    url: '/api/blade-martial/judge-project/detail',
    method: 'get',
    params: { id }
  })
}

/**
 * 分配裁判到项目
 * @param {Object} data - 分配数据
 * @param {Number} data.competitionId - 赛事ID
 * @param {Number} data.judgeId - 裁判ID
 * @param {String} data.judgeName - 裁判姓名
 * @param {Number} data.projectId - 项目ID
 * @param {String} data.projectName - 项目名称
 * @param {String} data.role - 角色（主裁、副裁、执裁）
 */
export const assignJudge = (data) => {
  return request({
    url: '/api/blade-martial/judge-project/assign',
    method: 'post',
    data
  })
}

/**
 * 修改分配
 * @param {Object} data - 分配数据
 */
export const updateAssignment = (data) => {
  return request({
    url: '/api/blade-martial/judge-project/update',
    method: 'post',
    data
  })
}

/**
 * 删除分配
 * @param {String} ids - 关联ID,多个用逗号分隔
 */
export const removeAssignment = (ids) => {
  return request({
    url: '/api/blade-martial/judge-project/remove',
    method: 'post',
    params: { ids }
  })
}

/**
 * 批量分配裁判
 * @param {Object} data - 批量分配参数
 * @param {Number} data.competitionId - 赛事ID
 * @param {Array} data.assignments - 分配信息数组
 * @param {Number} data.assignments[].judgeId - 裁判ID
 * @param {Number} data.assignments[].projectId - 项目ID
 * @param {String} data.assignments[].role - 角色
 */
export const batchAssignJudges = (data) => {
  return request({
    url: '/api/blade-martial/judge-project/batch-assign',
    method: 'post',
    data
  })
}

/**
 * 自动分配裁判
 * @param {Object} data - 自动分配参数
 * @param {Number} data.competitionId - 赛事ID
 * @param {Number} data.projectId - 项目ID（可选）
 * @param {String} data.strategy - 分配策略（balance负载均衡/level按等级）
 */
export const autoAssignJudges = (data) => {
  return request({
    url: '/api/blade-martial/judge-project/auto-assign',
    method: 'post',
    data
  })
}

/**
 * 获取项目的裁判列表
 * @param {Number} projectId - 项目ID
 */
export const getJudgesByProject = (projectId) => {
  return request({
    url: '/api/blade-martial/judge-project/judges-by-project',
    method: 'get',
    params: { projectId }
  })
}

/**
 * 获取裁判的项目列表
 * @param {Number} judgeId - 裁判ID
 * @param {Number} competitionId - 赛事ID
 */
export const getProjectsByJudge = (judgeId, competitionId) => {
  return request({
    url: '/api/blade-martial/judge-project/projects-by-judge',
    method: 'get',
    params: { judgeId, competitionId }
  })
}

/**
 * 获取裁判工作量统计
 * @param {Number} competitionId - 赛事ID
 */
export const getJudgeWorkload = (competitionId) => {
  return request({
    url: '/api/blade-martial/judge-project/workload',
    method: 'get',
    params: { competitionId }
  })
}

/**
 * 检查裁判冲突
 * @param {Object} data - 检查参数
 * @param {Number} data.judgeId - 裁判ID
 * @param {Number} data.projectId - 项目ID
 * @param {Number} data.competitionId - 赛事ID
 */
export const checkJudgeConflict = (data) => {
  return request({
    url: '/api/blade-martial/judge-project/check-conflict',
    method: 'post',
    data
  })
}

/**
 * 交换两个裁判的项目分配
 * @param {Object} data - 交换参数
 * @param {Number} data.assignmentId1 - 分配1的ID
 * @param {Number} data.assignmentId2 - 分配2的ID
 */
export const swapJudgeAssignment = (data) => {
  return request({
    url: '/api/blade-martial/judge-project/swap',
    method: 'post',
    data
  })
}

/**
 * 复制分配到其他项目
 * @param {Object} data - 复制参数
 * @param {Number} data.sourceProjectId - 源项目ID
 * @param {Number} data.targetProjectId - 目标项目ID
 */
export const copyAssignment = (data) => {
  return request({
    url: '/api/blade-martial/judge-project/copy',
    method: 'post',
    data
  })
}

/**
 * 获取可分配的裁判列表
 * @param {Number} competitionId - 赛事ID
 * @param {Number} projectId - 项目ID（可选）
 */
export const getAvailableJudges = (competitionId, projectId) => {
  return request({
    url: '/api/blade-martial/judge-project/available-judges',
    method: 'get',
    params: { competitionId, projectId }
  })
}

/**
 * 获取未分配裁判的项目列表
 * @param {Number} competitionId - 赛事ID
 */
export const getUnassignedProjects = (competitionId) => {
  return request({
    url: '/api/blade-martial/judge-project/unassigned-projects',
    method: 'get',
    params: { competitionId }
  })
}

/**
 * 导出裁判分配表
 * @param {Object} params - 导出参数
 */
export const exportAssignments = (params) => {
  return request({
    url: '/api/blade-martial/judge-project/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

/**
 * 导入裁判分配
 * @param {Number} competitionId - 赛事ID
 * @param {File} file - Excel文件
 */
export const importAssignments = (competitionId, file) => {
  const formData = new FormData()
  formData.append('competitionId', competitionId)
  formData.append('file', file)
  return request({
    url: '/api/blade-martial/judge-project/import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}
