/**
 * ERP API 配置管理接口定义
 */
import type { ID, IDS, PageQuery, PageResult } from '#/api/common';
import { requestClient } from '#/api/request';

enum Api {
  root = '/erp/api/config',
  test = '/erp/api/config/test',
  preview = '/erp/api/config/preview',
  import = '/erp/api/config/importFromTable',
  stats = '/erp/api/config/stats',
  errorLog = '/erp/api/config/errorLog',
  cache = '/erp/api/config/cache',
}

/**
 * 分页查询API配置列表
 */
export function apiConfigList(params?: PageQuery) {
  return requestClient.get<PageResult<ErpApiConfigVO>>(Api.root + '/list', { params });
}

/**
 * 获取API配置详情
 */
export function apiConfigInfo(apiId: ID) {
  return requestClient.get<ErpApiConfigInfoResponse>(`${Api.root}/${apiId}`);
}

/**
 * 新增API配置
 */
export function apiConfigAdd(data: Partial<ErpApiConfig>) {
  return requestClient.postWithMsg<void>(Api.root, data);
}

/**
 * 修改API配置
 */
export function apiConfigEdit(data: Partial<ErpApiConfig>) {
  return requestClient.putWithMsg<void>(Api.root, data);
}

/**
 * 删除API配置
 */
export function apiConfigRemove(apiIds: IDS) {
  return requestClient.deleteWithMsg<void>(`${Api.root}/${apiIds}`);
}

/**
 * 更新API状态
 */
export function apiConfigChangeStatus(data: Partial<ErpApiConfig>) {
  return requestClient.putWithMsg<void>(`${Api.root}/changeStatus`, data);
}

/**
 * 从表导入
 */
export function apiConfigImportFromTable(data: ImportTableRequest) {
  return requestClient.postWithMsg<void>(Api.import, data);
}

/**
 * 同步表结构
 */
export function apiConfigSyncTable(apiId: ID) {
  return requestClient.get<void>(`${Api.root}/syncTable/${apiId}`);
}

/**
 * API测试
 */
export function apiConfigTest(apiId: ID, params: Record<string, any>) {
  return requestClient.post<ApiTestResultVO>(`${Api.test}/${apiId}`, params);
}

/**
 * API文档预览
 */
export function apiConfigPreview(apiId: ID) {
  return requestClient.get<Record<string, string>>(`${Api.preview}/${apiId}`);
}

/**
 * 查询调用统计
 */
export function apiConfigStats(apiId: ID, startTime?: string, endTime?: string) {
  return requestClient.get<ApiStatsResponse>(`${Api.stats}/${apiId}`, {
    params: { startTime, endTime },
  });
}

/**
 * 查询错误日志
 */
export function apiConfigErrorLog(apiId: ID, limit?: number) {
  return requestClient.get<ApiErrorLogItem[]>(`${Api.errorLog}/${apiId}`, {
    params: { limit: limit || 10 },
  });
}

/**
 * 清除缓存
 */
export function apiConfigClearCache(apiId: ID) {
  return requestClient.deleteWithMsg<void>(`${Api.cache}/${apiId}`);
}

/**
 * API配置VO
 */
export interface ErpApiConfigVO {
  apiId: number;
  apiName: string;
  apiPath: string;
  apiMethod: string;
  apiDesc: string;
  apiVersion: string;
  dataSource: string;
  sqlTemplate: string;
  resultType: string;
  supportPagination: number;
  pageParamName: string;
  sizeParamName: string;
  requireAuth: number;
  permissionCode: string;
  enableCache: number;
  cacheKeyTemplate: string;
  cacheTtl: number;
  sourceTable: string;
  sourceTableComment: string;
  status: number;
  createTime: string;
  updateTime: string;
  createBy: string;
  updateBy: string;
  remark: string;
}

/**
 * API配置实体
 */
export interface ErpApiConfig extends ErpApiConfigVO {
  params?: ErpApiParam[];
}

/**
 * API参数配置
 */
export interface ErpApiParam {
  paramId: number;
  apiId: number;
  paramName: string;
  paramDesc: string;
  paramType: string;
  paramPosition: string;
  isRequired: number;
  defaultValue: string;
  sqlParamName: string;
  sort: number;
}

/**
 * API配置详情响应
 */
export interface ErpApiConfigInfoResponse {
  info: ErpApiConfig;
  params: ErpApiParam[];
}

/**
 * API测试结果
 */
export interface ApiTestResultVO {
  apiPath: string;
  testMethod: string;
  requestParams: Record<string, any>;
  success: boolean;
  data: any;
  executionTime: number;
  executedSql: string;
  errorMessage: string;
  errorStack: string;
}

/**
 * API统计响应
 */
export interface ApiStatsResponse {
  totalCalls: number;
  avgResponseTime: number;
  errorRate: number;
}

/**
 * API错误日志项
 */
export interface ApiErrorLogItem {
  statsId: number;
  apiId: number;
  callTime: string;
  callParams: string;
  responseTime: number;
  callStatus: string;
  errorMessage: string;
  errorStack: string;
  clientIp: string;
  userId: string;
}

/**
 * 从表导入请求
 */
export interface ImportTableRequest {
  tableNames: string[];
  dataSource?: string;
}