// CAD软件配置管理文件 - 统一管理所有CAD软件的配置和状态

// API基础配置
const API_CONFIG = {
  // 基础配置
  BASE_URL: import.meta.env.MODE === 'production' ? 'https://api.miany.com' : 'http://localhost',
  TIMEOUT: 30000, // 30秒超时

  // WebSocket配置
  WEBSOCKET: {
    BASE_URL: 'ws://localhost:8000',
    ENDPOINT: '/api/v1/ws/connect',
    CLIENT_ID: 'web_client',
    USER_ID: null, // 动态设置，从auth store获取
    RECONNECT_ATTEMPTS: 5,
    RECONNECT_DELAY: 1000, // 1秒
    HEARTBEAT_INTERVAL: 30000 // 30秒
  },

  // 通用API端点
  COMMON_ENDPOINTS: {
    health: '/health',
    version: '/version',
    ping: '/ping'
  },

  // 通知配置
  NOTIFICATION: {
    POSITION: 'top-right',
    SUCCESS_DURATION: 2000, // 成功通知2秒
    ERROR_DURATION: 3000,   // 错误通知3秒
    SUCCESS_LONG_DURATION: 5000, // 重要成功通知5秒
    SUCCESS_TITLE: '操作成功',
    ERROR_TITLE: '操作失败'
  },

  // 默认服务器配置
  DEFAULT_SERVER: {
    HOST: 'localhost',
    PORT: 8080
  },

  // 导出格式支持
  EXPORT_FORMATS: {
    IFC: { extension: 'ifc', name: 'IFC 格式' },
    STP: { extension: 'stp', name: 'STP 格式' }
  },

  // 几何优化默认参数
  GEOMETRY_OPTIMIZATION: {
    SOFTWARE_TYPE: "creo",
    METHOD: "outer_shell",
    QUALITY: 5,
    CHORD_HEIGHT: 0.15,
    FILL_HOLES: false,
    IGNORE_SMALL_SURFACES: false,
    SMALL_SURFACE_PERCENTAGE: 0.0,
    OUTPUT_TYPE: "solid_surface",
    IGNORE_QUILTS: false,
    IGNORE_SKELETON: false,
    ASSIGN_MASS_PROPERTIES: false
  }
}

// CAD软件完整定义配置 - 整合显示信息和API配置
const CAD_SOFTWARE_DEFINITIONS = {
  CREO: {
    id: 'creo',
    name: 'Creo Parametric',
    displayName: 'Creo',
    icon: 'fas fa-cogs',
    version: '9.0',
    port: 12345,
    baseUrl: 'http://localhost:12345',
    apiVersion: 'v1',
    priority: 1,
    description: '主要CAD软件',
    features: [
      'hierarchy', 'geometryComplexity', 'shellAnalysis',
      'optimization', 'deleteComponent', 'export'
    ],
    endpoints: {
      connect: '/test',
      status: '/api/status/model',
      open: '/api/model/open',
      hierarchy: '/api/creo/analysis/hierarchy',
      hierarchyStatistics: '/api/analysis/hierarchy-statistics',
      hierarchyDelete: '/api/creo/hierarchy/delete',
      geometryComplexity: '/api/analysis/geometry-complexity',
      shellAnalysis: '/api/analysis/shell-analysis',
      optimization: '/api/creo/shrinkwrap/shell',
      deleteComponent: '/api/creo/component/delete-by-path',
      componentChildren: '/api/creo/component/children',
      export: '/api/export/model'
    }
  },
  REVIT: {
    id: 'revit',
    name: 'Revit',
    displayName: 'Revit',
    icon: 'fas fa-building',
    version: '2024',
    port: 9000,
    baseUrl: 'http://localhost:9000',
    apiVersion: 'v1',
    priority: 2,
    description: 'BIM模型管理',
    features: ['overview', 'shellAnalysis', 'exportIfc'],
    endpoints: {
      connect: '/api/health',
      overview: '/api/overview',
      shellAnalysis: '/api/shell/analyze',
      exportIfc: '/api/export/ifc'
    }
  },
  PDMS: {
    id: 'pdms',
    name: 'PDMS',
    displayName: 'PDMS',
    icon: 'fas fa-industry',
    version: '12.1',
    port: 9001,
    baseUrl: 'http://localhost:9001',
    apiVersion: 'v1',
    priority: 3,
    description: '工厂设计管理',
    features: ['status', 'piping'],
    endpoints: {
      connect: '/test',
      status: '/api/status/model',
      piping: '/api/v1/piping'
    }
  },
  AUTOCAD: {
    id: 'autocad',
    name: 'AutoCAD',
    displayName: 'AutoCAD',
    icon: 'fas fa-drafting-compass',
    version: '2024',
    port: 8080,
    baseUrl: 'http://localhost:8080',
    apiVersion: 'v1',
    priority: 4,
    description: '连接和模型管理',
    features: [],
    endpoints: {}
  },
  SOLIDWORKS: {
    id: 'solidworks',
    name: 'SolidWorks',
    displayName: 'SolidWorks',
    icon: 'fas fa-cube',
    version: '2023',
    port: 8081,
    baseUrl: 'http://localhost:8081',
    apiVersion: 'v1',
    priority: 5,
    description: '装配体管理',
    features: [],
    endpoints: {}
  },
  CATIA: {
    id: 'catia',
    name: 'CATIA',
    displayName: 'CATIA',
    icon: 'fas fa-hammer',
    version: 'V5-6',
    port: 8082,
    baseUrl: 'http://localhost:8082',
    apiVersion: 'v1',
    priority: 6,
    description: '连接和模型管理',
    features: [],
    endpoints: {}
  }
}


// 获取所有CAD软件定义
export const getAllCADDefinitions = () => {
  return Object.values(CAD_SOFTWARE_DEFINITIONS)
    .sort((a, b) => a.priority - b.priority)
}


// 根据ID获取CAD定义
export const getCADDefinitionById = (id) => {
  return getAllCADDefinitions().find(cad => cad.id === id)
}

// 根据名称获取CAD定义
export const getCADDefinitionByName = (name) => {
  return getAllCADDefinitions().find(cad =>
    cad.name === name || cad.displayName === name
  )
}

// 获取可显示的CAD列表
export const getDisplayableCADs = () => {
  return getAllCADDefinitions()
}

// 检查CAD是否支持特定功能
export const cadSupportsFeature = (cadId, feature) => {
  const cad = getCADDefinitionById(cadId)
  return cad ? cad.features.includes(feature) : false
}


// ===== API相关工具函数 (从原api.js合并) =====

// 获取CAD服务配置
export const getCADServiceConfig = (cadName) => {
  const serviceKey = cadName.toUpperCase().replace(/\s+/g, '')
  return CAD_SOFTWARE_DEFINITIONS[serviceKey] || null
}

// 构建完整的API URL
export const buildApiUrl = (cadName, endpoint) => {
  const service = getCADServiceConfig(cadName)
  if (!service) {
    throw new Error(`不支持的CAD软件: ${cadName}`)
  }

  const endpointPath = service.endpoints[endpoint]
  if (!endpointPath) {
    throw new Error(`不支持的端点: ${endpoint}`)
  }

  return `${service.baseUrl}${endpointPath}`
}

// 获取所有CAD服务列表
export const getAllCADServices = () => {
  return Object.values(CAD_SOFTWARE_DEFINITIONS)
}

// 获取支持的导出格式
export const getExportFormats = () => {
  return API_CONFIG.EXPORT_FORMATS
}

// 获取通知配置
export const getNotificationConfig = () => {
  return API_CONFIG.NOTIFICATION
}

// 获取默认服务器配置
export const getDefaultServerConfig = () => {
  return API_CONFIG.DEFAULT_SERVER
}

// 获取几何优化默认参数
export const getGeometryOptimizationDefaults = () => {
  return API_CONFIG.GEOMETRY_OPTIMIZATION
}

// 获取WebSocket配置
export const getWebSocketConfig = () => {
  return API_CONFIG.WEBSOCKET
}

// HTTP请求头配置
export const getRequestHeaders = () => {
  return {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  }
}

// 默认请求配置
export const getDefaultRequestConfig = () => {
  return {
    timeout: API_CONFIG.TIMEOUT,
    headers: getRequestHeaders()
  }
}

// 默认导出配置对象
export default {
  CAD_SOFTWARE_DEFINITIONS,
  API_CONFIG,
  getAllCADDefinitions,
  getDisplayableCADs,
  getCADServiceConfig,
  buildApiUrl,
  getAllCADServices,
  getExportFormats,
  getRequestHeaders,
  getDefaultRequestConfig
}