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

/**
 * 分页查询应用成员
 */
export async function pageAppUser(params: AppUserParam) {
  const res = await request.get<ApiResult<PageResult<AppUser>>>(
    '/app/app-user/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询应用成员列表
 */
export async function listAppUser(params?: AppUserParam) {
  const res = await request.get<ApiResult<AppUser[]>>(
    '/app/app-user',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加应用成员
 */
export async function addAppUser(data: AppUser) {
  const res = await request.post<ApiResult<unknown>>(
    '/app/app-user',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改应用成员
 */
export async function updateAppUser(data: AppUser) {
  const res = await request.put<ApiResult<unknown>>(
    '/app/app-user',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除应用成员
 */
export async function removeAppUser(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/app/app-user/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

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

/**
 * 根据id查询应用成员
 */
export async function getAppUser(id: number) {
  const res = await request.get<ApiResult<AppUser>>(
    '/app/app-user/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
