import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { ShopEvent, ShopEventParam, ShopEventRegistration, ShopEventRegistrationParam } from './model';

export type { ShopEvent, ShopEventParam, ShopEventRegistration, ShopEventRegistrationParam };

/**
 * 分页查询赛事
 */
export async function pageShopEvent(params: ShopEventParam) {
  const res = await request.get<ApiResult<PageResult<ShopEvent>>>(
    '/shop/shop-event/page',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询赛事列表
 */
export async function listShopEvent(params?: ShopEventParam) {
  const res = await request.get<ApiResult<ShopEvent[]>>(
    '/shop/shop-event',
    { params }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询赛事
 */
export async function getShopEvent(id: number) {
  const res = await request.get<ApiResult<ShopEvent>>(
    '/shop/shop-event/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加赛事
 */
export async function addShopEvent(data: ShopEvent) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-event',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改赛事
 */
export async function updateShopEvent(data: ShopEvent) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-event',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除赛事
 */
export async function removeShopEvent(id: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/shop/shop-event/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除赛事
 */
export async function removeBatchShopEvent(ids: number[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/shop/shop-event/batch',
    { data: ids }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 分页查询赛事报名记录
 */
export async function pageShopEventRegistration(params: ShopEventRegistrationParam) {
  const res = await request.get<ApiResult<PageResult<ShopEventRegistration>>>(
    '/shop/shop-event-registration/page',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据赛事ID查询报名记录
 */
export async function listShopEventRegistration(eventId: number) {
  const res = await request.get<ApiResult<ShopEventRegistration[]>>(
    '/shop/shop-event-registration',
    { params: { eventId } }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
