import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api/index';
import type { AppVersion, AppVersionParam } from './model';

/**
 * 分页查询应用版本发布记录
 */
export async function pageAppVersion(params: AppVersionParam) {
  const res = await request.get<ApiResult<PageResult<AppVersion>>>(
    '/app/app-version/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询应用版本发布记录列表
 */
export async function listAppVersion(params?: AppVersionParam) {
  const res = await request.get<ApiResult<AppVersion[]>>(
    '/app/app-version',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加应用版本发布记录
 */
export async function addAppVersion(data: AppVersion) {
  const res = await request.post<ApiResult<unknown>>(
    '/app/app-version',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改应用版本发布记录
 */
export async function updateAppVersion(data: AppVersion) {
  const res = await request.put<ApiResult<unknown>>(
    '/app/app-version',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除应用版本发布记录
 */
export async function removeAppVersion(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/app/app-version/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除应用版本发布记录
 */
export async function removeBatchAppVersion(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/app/app-version/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询应用版本发布记录
 */
export async function getAppVersion(id: number) {
  const res = await request.get<ApiResult<AppVersion>>(
    '/app/app-version/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
