import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import {
  TournamentsVO,
  TournamentsForm,
  TournamentsQuery,
  TournamentsBindStructuresQuery,
  TournamentsBindStructuresVO
} from '@/api/system/business/tournamentsTemplate/types';

/**
 * 查询【请填写功能名称】列表
 * @param query
 * @returns {*}
 */

export const listTournaments = (query?: TournamentsQuery): AxiosPromise<TournamentsVO[]> => {
  return request({
    url: '/business/tournamentsTemplate/list',
    method: 'get',
    params: query
  });
};

/**
 * 查询【请填写功能名称】详细
 * @param id
 */
export const getTournaments = (id: string | number): AxiosPromise<TournamentsVO> => {
  return request({
    url: '/business/tournamentsTemplate/' + id,
    method: 'get'
  });
};

/**
 * 新增【请填写功能名称】
 * @param data
 */
export const addTournaments = (data: TournamentsForm) => {
  return request({
    url: '/business/tournamentsTemplate',
    method: 'post',
    data: data
  });
};

/**
 * 修改【请填写功能名称】
 * @param data
 */
export const updateTournaments = (data: TournamentsForm) => {
  return request({
    url: '/business/tournamentsTemplate',
    method: 'put',
    data: data
  });
};

/**
 * 删除【请填写功能名称】
 * @param id
 */
export const delTournaments = (id: string | number | Array<string | number>) => {
  return request({
    url: '/business/tournamentsTemplate/' + id,
    method: 'delete'
  });
};

export const getSelectTournamentBlindStructuresList = (query?: TournamentsBindStructuresQuery): AxiosPromise<TournamentsBindStructuresVO[]> => {
  return request({
    url: '/business/tournamentsTemplate/selectTournamentBlindStructuresList',
    method: 'get',
    params: query
  });
};

/**
 * 分配盲注给赛事
 * @param data 请求数据 { tournamentId, blindStructureIds }
 */
export const assignTournamentBlindStructures = (data: { tournamentId: number; blindStructureIds: number[] }): AxiosPromise<void> => {
  return request({
    url: '/business/tournamentsTemplate/assignTournamentBlindStructures',
    method: 'post',
    data: data
  });
};

/**
 * 上传 比赛图标
 * @param file 文件
 */
export const uploadTournament = async (file: File) => {
  const formData = new FormData();
  formData.append('file', file); // 后端接收的参数名是 "file"
  const res = await request({
    url: '/business/tournamentsTemplate/uploadTournament',
    method: 'POST',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });

  return res;
};
export const selectTournamentsVoListTemplateList = (): AxiosPromise<TournamentsVO> => {
  return request({
    url: '/business/tournamentsTemplate/selectTournamentsVoListTemplateList',
    method: 'get'
  });
};
/**
 * 通过模板生成赛事
 * @param data 请求数据 { templateId, beginTime }
 */
export const generateTournamentByTemplate = (data: { templateId: number; beginTime: string }): AxiosPromise<void> => {
  return request({
    url: '/business/tournamentsTemplate/generateTournamentByTemplate',
    method: 'post',
    params: data // 将数据作为查询参数传递
  });
};
