import { http } from "@/utils/http";
import type {
  CacheItem,
  CacheList,
  OperationLogInfo,
  RedisMonitorInfo,
  SystemMonitorInfo,
  UserLoginLogInfo
} from "types/monitor";
import { filterEmptyObject } from "./utils";

// --------------------------登录日志相关--------------------------------------
/**
 * 用户获取登录日志
 */

export const getUserLoginLogAPI = (params: {
  page: number;
  pageSize: number;
}) => {
  return http.request<QueryListResult<UserLoginLogInfo>>(
    "get",
    "/api/log/login",
    {
      params: filterEmptyObject(params)
    }
  );
};

/**
 * 用户强退
 * @param id
 * @returns
 */
export const deleteUserOnlineAPI = (id: string) => {
  return http.request<null>("delete", `/api/log/logout/${id}`);
};

// ------------------------操作日志相关----------------------------------------

/**
 * 获取用户操作日志
 */
export const getUserOperationsAPI = (params: {
  page: number;
  pageSize: number;
}) => {
  return http.request<QueryListResult<OperationLogInfo>>(
    "GET",
    "/api/log/operation",
    {
      params
    }
  );
};

// ------------------------服务相关----------------------------------------

/**
 * 获取服务器信息
 */
export const getSystemMonitorInfoAPI = () => {
  return http.request<SystemMonitorInfo>("get", "/api/server");
};

// --------------------------缓存相关--------------------------------------

/**
 * 获取服务器缓存信息
 * @returns
 */
export const getCachedMonitorInfoAPI = () => {
  return http.request<RedisMonitorInfo>("get", "/api/cache/monitor");
};

/**
 * 获取缓存名称列表
 */
export const getCachedNamesAPI = () => {
  return http.request<CacheList>("get", "/api/cache/names");
};

/**获取缓存键名列表 */
export const getCachedKeysAPI = (cacheName: string) => {
  return http.request<string[]>("get", `/api/cache/keys/${cacheName}`);
};

/**获取缓存详细信息 */
export const getCachedInfoAPI = (cacheName: string, cacheKey: string) => {
  return http.request<CacheItem>(
    "get",
    `/api/cache/info/${cacheName}/${cacheKey}`
  );
};

/**通过键名删除缓存 */
export const deleteCachedAPI = (name: string) => {
  return http.request<null>("delete", `/api/cache/cacheName/${name}`);
};

/**通过键值删除缓存 */
export const deleteCachedKeyAPI = (key: string) => {
  return http.request<null>("delete", `/api/cache/cacheKey/${key}`);
};

/**删除所有缓存 */
export const deleteAllCachedAPI = () => {
  return http.request<null>("delete", `/api/cache/clearAll`);
};
