export const elasticsearchSchema = {
  id: 'elasticsearch',
  name: 'Elasticsearch',
  icon: 'Search',
  category: '搜索引擎',
  description: '分布式全文搜索和分析引擎',
  format: 'yml',
  fileName: 'elasticsearch.yml',
  groups: [
    {
      title: '集群配置',
      fields: [
        {
          key: 'clusterName',
          label: '集群名称',
          type: 'text',
          default: 'my-cluster',
        },
        {
          key: 'nodeName',
          label: '节点名称',
          type: 'text',
          default: 'node-1',
        },
        {
          key: 'nodeRoles',
          label: '节点角色',
          type: 'select',
          options: [
            { label: '主节点+数据+预处理+ML (默认)', value: 'master,data,ingest,ml' },
            { label: '主节点+数据', value: 'master,data' },
            { label: '仅主节点', value: 'master' },
            { label: '仅数据节点', value: 'data' },
            { label: '仅协调节点', value: 'ingest' },
          ],
          default: 'master,data,ingest,ml',
          tip: '生产环境建议主节点和数据节点分离',
        },
      ],
    },
    {
      title: '网络配置',
      fields: [
        {
          key: 'networkHost',
          label: '绑定地址',
          type: 'text',
          default: '0.0.0.0',
        },
        {
          key: 'httpPort',
          label: 'HTTP 端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 9200,
        },
        {
          key: 'transportPort',
          label: '传输端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 9300,
        },
      ],
    },
    {
      title: '节点发现',
      fields: [
        {
          key: 'discoveryType',
          label: '发现类型',
          type: 'select',
          options: [
            { label: 'single-node - 单节点模式', value: 'single-node' },
            { label: 'zen - 集群模式', value: 'zen' },
          ],
          default: 'single-node',
        },
        {
          key: 'initialMasterNodes',
          label: '初始主节点列表',
          type: 'text',
          default: 'node-1',
          tip: '仅首次启动集群时需要',
          dependsOn: { key: 'discoveryType', value: 'zen' },
        },
      ],
    },
    {
      title: 'JVM 内存',
      fields: [
        {
          key: 'heapSize',
          label: 'JVM 堆内存',
          type: 'select',
          options: [
            { label: '1g', value: '1g' },
            { label: '2g', value: '2g' },
            { label: '4g', value: '4g' },
            { label: '8g', value: '8g' },
          ],
          default: '1g',
          tip: '建议不超过物理内存的 50%，且不超过 32GB',
        },
      ],
    },
    {
      title: '安全配置',
      fields: [
        {
          key: 'enableXpack',
          label: '开启 X-Pack',
          type: 'switch',
          default: false,
        },
        {
          key: 'enableSecurity',
          label: '开启安全功能',
          type: 'switch',
          default: false,
          dependsOn: { key: 'enableXpack', value: true },
        },
        {
          key: 'enableSsl',
          label: '开启 SSL',
          type: 'switch',
          default: false,
          dependsOn: { key: 'enableSecurity', value: true },
        },
      ],
    },
    {
      title: '索引性能',
      fields: [
        {
          key: 'indicesMemoryIndexBufferSize',
          label: '索引缓冲区比例',
          type: 'select',
          options: [
            { label: '10%', value: '10%' },
            { label: '15%', value: '15%' },
            { label: '20% (推荐)', value: '20%' },
            { label: '25%', value: '25%' },
          ],
          default: '20%',
          tip: '堆内存中用于索引缓冲的比例',
        },
        {
          key: 'maxResultWindow',
          label: '最大结果窗口',
          type: 'number',
          min: 1000,
          max: 100000,
          default: 10000,
          tip: 'from+size 的最大值',
        },
      ],
    },
    {
      title: '慢日志',
      fields: [
        {
          key: 'enableSlowLog',
          label: '开启慢日志',
          type: 'switch',
          default: false,
        },
        {
          key: 'slowLogThresholdTrace',
          label: 'Trace 级别阈值',
          type: 'text',
          default: '500ms',
          dependsOn: { key: 'enableSlowLog', value: true },
        },
      ],
    },
    {
      title: '路径配置',
      fields: [
        {
          key: 'pathData',
          label: '数据目录',
          type: 'text',
          default: '/var/lib/elasticsearch',
        },
        {
          key: 'pathLogs',
          label: '日志目录',
          type: 'text',
          default: '/var/log/elasticsearch',
        },
      ],
    },
  ],
}

export function generateElasticsearchYml(config) {
  const lines = []
  lines.push(`# Elasticsearch 配置文件 - 由 ConfTemplate 生成`)
  lines.push(`# 生成时间: ${new Date().toLocaleString('zh-CN')}`)
  lines.push(``)

  lines.push(`# ======================== 集群配置 ========================`)
  lines.push(`cluster.name: ${config.clusterName}`)
  lines.push(`node.name: ${config.nodeName}`)
  lines.push(`node.roles: [${config.nodeRoles}]`)
  lines.push(``)

  lines.push(`# ======================== 网络配置 ========================`)
  lines.push(`network.host: ${config.networkHost}`)
  lines.push(`http.port: ${config.httpPort}`)
  lines.push(`transport.port: ${config.transportPort}`)
  lines.push(``)

  lines.push(`# ======================== 节点发现 ========================`)
  lines.push(`discovery.type: ${config.discoveryType}`)
  if (config.discoveryType === 'zen') {
    lines.push(`cluster.initial_master_nodes: [${config.initialMasterNodes}]`)
  }
  lines.push(``)

  lines.push(`# ======================== 路径配置 ========================`)
  lines.push(`path.data: ${config.pathData}`)
  lines.push(`path.logs: ${config.pathLogs}`)
  lines.push(``)

  lines.push(`# ======================== 索引性能 ========================`)
  lines.push(`indices.memory.index_buffer_size: ${config.indicesMemoryIndexBufferSize}`)
  lines.push(`index.max_result_window: ${config.maxResultWindow}`)
  lines.push(``)

  if (config.enableXpack) {
    lines.push(`# ======================== X-Pack 配置 ========================`)
    lines.push(`xpack.security.enabled: ${config.enableSecurity}`)
    if (config.enableSecurity && config.enableSsl) {
      lines.push(`xpack.security.transport.ssl.enabled: true`)
      lines.push(`xpack.security.http.ssl.enabled: true`)
    }
    lines.push(``)
  }

  if (config.enableSlowLog) {
    lines.push(`# ======================== 慢日志配置 ========================`)
    lines.push(`index.search.slowlog.threshold.query.trace: ${config.slowLogThresholdTrace}`)
    lines.push(`index.search.slowlog.threshold.fetch.trace: ${config.slowLogThresholdTrace}`)
    lines.push(`index.indexing.slowlog.threshold.index.trace: ${config.slowLogThresholdTrace}`)
    lines.push(``)
  }

  return lines.join('\n')
}
