/**
 * KRA - API Management Service
 */
import request from '@/utils/request';

export interface ApiItem {
  ID?: number;
  path: string;
  description: string;
  apiGroup: string;
  method: string;
}

/** 获取API列表 */
export const getApiList = (data: { page: number; pageSize: number; path?: string; description?: string; method?: string; apiGroup?: string; orderKey?: string; desc?: boolean }) => {
  return request.post('/api/getApiList', data);
};

/** 创建API */
export const createApi = (data: ApiItem) => {
  return request.post('/api/createApi', data);
};

/** 根据ID获取API */
export const getApiById = (data: { id: number }) => {
  return request.post('/api/getApiById', data);
};

/** 更新API */
export const updateApi = (data: ApiItem) => {
  return request.post('/api/updateApi', data);
};

/** 设置API权限 */
export const setAuthApi = (data: any) => {
  return request.post('/api/setAuthApi', data);
};

/** 获取所有API（不分页） */
export const getAllApis = (data?: any) => {
  return request.post('/api/getAllApis', data);
};

/** 删除API */
export const deleteApi = (data: { ID: number }) => {
  return request.post('/api/deleteApi', data);
};

/** 批量删除API */
export const deleteApisByIds = (data: { ids: number[] }) => {
  return request.delete('/api/deleteApisByIds', { data });
};

/** 刷新Casbin缓存 */
export const freshCasbin = () => {
  return request.get('/api/freshCasbin');
};

/** 同步API */
export const syncApi = () => {
  return request.get('/api/syncApi');
};

/** 获取API分组 */
export const getApiGroups = () => {
  return request.get('/api/getApiGroups');
};

/** 忽略API */
export const ignoreApi = (data: any) => {
  return request.post('/api/ignoreApi', data);
};

/** 进入同步API */
export const enterSyncApi = (data: any) => {
  return request.post('/api/enterSyncApi', data);
};

export default {
  getApiList,
  createApi,
  getApiById,
  updateApi,
  setAuthApi,
  getAllApis,
  deleteApi,
  deleteApisByIds,
  freshCasbin,
  syncApi,
  getApiGroups,
  ignoreApi,
  enterSyncApi,
};
