import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { GameScoreVO, GameScoreForm, GameScoreQuery } from '@/api/system/gameScore/types';

/**
 * 查询成绩列表
 * @param query
 * @returns {*}
 */

export const listGameScore = (query?: GameScoreQuery): AxiosPromise<GameScoreVO[]> => {
  return request({
    url: '/system/gameScore/list',
    method: 'get',
    params: query
  });
};

/**
 * 查询成绩详细
 * @param scoreId
 */
export const getGameScore = (scoreId: string | number): AxiosPromise<GameScoreVO> => {
  return request({
    url: '/system/gameScore/' + scoreId,
    method: 'get'
  });
};

/**
 * 根据运动员ID和项目ID查询成绩
 * @param athleteId
 * @param projectId
 */
export const getScoreByAthleteIdAndProjectId = (athleteId: string | number, projectId: string | number): AxiosPromise<GameScoreVO> => {
  return request({
    url: '/system/gameScore/getScoreByAthleteIdAndProjectId',
    method: 'get',
    params: { athleteId, projectId }
  });
};

/**
 * 查询项目成绩详细
 * @param projectId
 */
export const getGameScoreByProjectId = (projectId: string | number): AxiosPromise<GameScoreVO> => {
  return request({
    url: '/system/gameScore/' + projectId,
    method: 'get'
  });
};

/**
 * 新增成绩
 * @param data
 */
export const addGameScore = (data: GameScoreForm) => {
  return request({
    url: '/system/gameScore',
    method: 'post',
    data: data
  });
};

/**
 * 修改成绩
 * @param data
 */
export const updateGameScore = (data: GameScoreForm) => {
  return request({
    url: '/system/gameScore',
    method: 'put',
    data: data
  });
};

/**
 * 删除成绩
 * @param scoreId
 */
export const delGameScore = (scoreId: string | number | Array<string | number>) => {
  return request({
    url: '/system/gameScore/' + scoreId,
    method: 'delete'
  });
};

/**
 * 获取项目成绩综合数据（包含运动员/队伍信息）
 * @param eventId 赛事ID
 * @param projectId 项目ID
 * @param classification 项目分类
 * @param searchValue 搜索值
 * @param pageQuery 分页参数
 */
export const getProjectScoreData = (params: {
  eventId: string | number;
  projectId: string | number;
  classification: string;
  searchValue?: string;
  pageNum?: number;
  pageSize?: number;
}) => {
  return request({
    url: '/system/gameScore/getProjectScoreData',
    method: 'get',
    params
  });
};

/**
 * 获取看板排名数据（极简接口，后端完成排序和排名）
 * @param params
 */
export const getRankingBoardData = (params: {
  eventId: string | number;
  projectId: string | number;
  classification: string;
  rgId?: string | number;
}) => {
  return request({
    url: '/system/gameScore/rankingBoard',
    method: 'get',
    params
  });
};

/**
 * 更新成绩并重新计算排名积分
 * @param data 成绩数据
 */
export const updateScoreAndRecalculate = (data: GameScoreForm) => {
  return request({
    url: '/system/gameScore/updateScoreAndRecalculate',
    method: 'put',
    data: data
  });
};

/**
 * 重新计算项目排名和积分
 * @param eventId 赛事ID
 * @param projectId 项目ID
 */
export const recalculateRankingsAndPoints = (eventId: string | number, projectId: string | number) => {
  return request({
    url: '/system/gameScore/recalculateRankingsAndPoints',
    method: 'post',
    params: { eventId, projectId }
  });
};

/**
 * 导出成绩汇总表
 * @param eventId 赛事ID
 */
export const exportScoresSummary = (eventId: string | number) => {
  return request({
    url: '/system/gameScore/exportScoresSummary',
    method: 'post',
    params: { eventId },
    responseType: 'blob' // 确保以二进制流形式接收响应
  });
};

/**
 * 获取加分数据
 * @param params 查询参数
 */
export const getBonusData = (params: { eventId: string | number; rgId?: string | number }) => {
  return request({
    url: '/system/gameScore/getBonusData',
    method: 'get',
    params
  });
};

/**
 * 更新加分数据
 * @param data 加分数据
 */
export const updateBonusData = (data: { eventId: string | number; data: any[] }) => {
  return request({
    url: '/system/gameScore/updateBonusData',
    method: 'put',
    data
  });
};

/**
 * 导出加分Excel
 * @param data 导出数据
 */
export const exportBonusExcel = (data: {
  eventId: string | number;
  data: any[];
  projects: any[]
}) => {
  return request({
    url: '/system/gameScore/exportBonusExcel',
    method: 'post',
    data,
    responseType: 'blob' // 确保以二进制流形式接收响应
  });
};

/**
 * 导出单个项目成绩详情
 * @param eventId 赛事ID
 * @param projectId 项目ID
 * @param topN 导出前几名
 */
export const exportProjectScore = (eventId: string | number, projectId: string | number, topN?: number) => {
  return request({
    url: '/system/gameScore/exportProjectScore',
    method: 'post',
    params: { eventId, projectId, topN },
    responseType: 'blob'
  });
};

/**
 * 导入成绩
 * @param data 导入数据
 */
export const importScore = (data: any) => {
  return request({
    url: '/system/gameScore/import',
    method: 'post',
    data: data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
};

/**
 * 下载导入模板
 * @param eventId 赛事ID
 * @param projectId 项目ID
 * @param classification 项目分类
 */
export const importTemplate = (eventId: string | number, projectId: string | number, classification: string) => {
  return request({
    url: '/system/gameScore/importTemplate',
    method: 'post',
    params: { eventId, projectId, classification },
    responseType: 'blob'
  });
};
