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

/**
 * 分页查询应用密钥凭证
 */
export async function pageAppCredential(params: AppCredentialParam) {
  const res = await request.get<ApiResult<PageResult<AppCredential>>>(
    '/app/app-credential/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询应用密钥凭证列表
 */
export async function listAppCredential(params?: AppCredentialParam) {
  const res = await request.get<ApiResult<AppCredential[]>>(
    '/app/app-credential',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加应用密钥凭证
 */
export async function addAppCredential(data: AppCredential) {
  const res = await request.post<ApiResult<unknown>>(
    '/app/app-credential',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改应用密钥凭证
 */
export async function updateAppCredential(data: AppCredential) {
  const res = await request.put<ApiResult<unknown>>(
    '/app/app-credential',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

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

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

/**
 * 根据id查询应用密钥凭证
 */
export async function getAppCredential(id: number) {
  const res = await request.get<ApiResult<AppCredential>>(
    '/app/app-credential/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
