/**
 * 赛程编排相关API接口
 */

import request from '@/utils/request.js'

/**
 * 获取赛程编排结果
 * @param {Number} competitionId - 赛事ID
 */
export function getScheduleResult(competitionId) {
  return request.get('/martial/schedule/result', {
    params: { competitionId }
  })
}

/**
 * 触发自动编排
 * @param {Number} competitionId - 赛事ID
 */
export function triggerAutoArrange(competitionId) {
  return request.post('/martial/schedule/auto-arrange', {
    competitionId
  })
}

/**
 * 保存编排草稿
 * @param {Object} data - 编排草稿数据
 * @param {Number} data.competitionId - 赛事ID
 * @param {Boolean} data.isDraft - 是否为草稿
 * @param {Array} data.competitionGroups - 竞赛分组数据
 */
export function saveDraftSchedule(data) {
  return request.post('/martial/schedule/save-draft', data)
}

/**
 * 保存并锁定赛程编排
 * @param {Number} competitionId - 赛事ID
 */
export function saveAndLockSchedule(competitionId) {
  return request.post('/martial/schedule/save-and-lock', {
    competitionId
  })
}

/**
 * 移动赛程分组到指定场地和时间段
 * @param {Object} data - 移动请求数据
 * @param {Number} data.groupId - 分组ID
 * @param {Number} data.targetVenueId - 目标场地ID
 * @param {Number} data.targetTimeSlotIndex - 目标时间段索引
 */
export function moveScheduleGroup(data) {
  return request.post('/martial/schedule/move-group', data)
}

/**
 * 获取调度数据
 * @param {Object} params - 查询参数
 * @param {Number} params.competitionId - 赛事ID
 * @param {Number} params.venueId - 场地ID
 * @param {Number} params.timeSlotIndex - 时间段索引
 */
export function getDispatchData(params) {
  return request.get('/martial/schedule/dispatch-data', {
    params
  })
}

/**
 * 调整出场顺序
 * @param {Object} data - 调整请求数据
 * @param {Number} data.detailId - 编排明细ID
 * @param {Number} data.participantId - 参赛者记录ID
 * @param {String} data.action - 调整动作(move_up/move_down/swap)
 * @param {Number} data.targetOrder - 目标顺序（交换时使用）
 */
export function adjustOrder(data) {
  return request.post('/martial/schedule/adjust-order', data)
}

/**
 * 批量保存调度
 * @param {Object} data - 保存调度数据
 * @param {Number} data.competitionId - 赛事ID
 * @param {Array} data.adjustments - 调整列表
 */
export function saveDispatch(data) {
  return request.post('/martial/schedule/save-dispatch', data)
}

export default {
  getScheduleResult,
  triggerAutoArrange,
  saveDraftSchedule,
  saveAndLockSchedule,
  moveScheduleGroup,
  getDispatchData,
  adjustOrder,
  saveDispatch
}
