/**
 * KRA - System Config API Service
 */
import request from '@/utils/request';
import type { ApiResponse } from '@/types/api';

export interface SystemConfig {
  system?: {
    env?: string;
    addr?: number;
    dbType?: string;
    ossType?: string;
    useMultipoint?: boolean;
    useRedis?: boolean;
    lruCacheSize?: number;
    iplimitCount?: number;
    iplimitTime?: number;
    routerPrefix?: string;
  };
  jwt?: {
    signingKey?: string;
    expiresTime?: string;
    bufferTime?: string;
    issuer?: string;
  };
  captcha?: {
    keyLong?: number;
    imgWidth?: number;
    imgHeight?: number;
    openCaptcha?: number;
    openCaptchaTimeOut?: number;
  };
  email?: {
    to?: string;
    port?: number;
    from?: string;
    host?: string;
    isSSL?: boolean;
    secret?: string;
    nickname?: string;
  };
  [key: string]: any;
}

export interface ServerInfo {
  cpu?: {
    cpus?: any[];
    cores?: number;
    usedPercent?: number;
  };
  ram?: {
    totalMb?: number;
    usedMb?: number;
    usedPercent?: number;
  };
  disk?: {
    totalMb?: number;
    usedMb?: number;
    usedPercent?: number;
  };
  os?: {
    goos?: string;
    numCpu?: number;
    compiler?: string;
    goVersion?: string;
    numGoroutine?: number;
  };
}

/** 获取系统配置 */
export const getSystemConfig = () => {
  return request.post<ApiResponse<{ config: SystemConfig }>>('/system/getSystemConfig');
};

/** 设置系统配置 */
export const setSystemConfig = (data: SystemConfig) => {
  return request.post<ApiResponse<any>>('/system/setSystemConfig', data);
};

/** 获取服务器运行状态 */
export const getServerInfo = () => {
  return request.post<ApiResponse<ServerInfo>>('/system/getServerInfo');
};

/** 重载服务 */
export const reloadSystem = (data?: any) => {
  return request.post<ApiResponse<any>>('/system/reloadSystem', data);
};

export default {
  getSystemConfig,
  setSystemConfig,
  getServerInfo,
  reloadSystem,
};
