export const kibanaSchema = {
  id: 'kibana',
  name: 'Kibana',
  icon: 'DataAnalysis',
  category: '日志监控',
  description: 'Elasticsearch 数据可视化与探索平台',
  format: 'yml',
  fileName: 'kibana.yml',
  groups: [
    {
      title: '服务配置',
      fields: [
        {
          key: 'serverPort',
          label: '服务端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 5601,
        },
        {
          key: 'serverHost',
          label: '绑定地址',
          type: 'text',
          default: '0.0.0.0',
          tip: '0.0.0.0 允许外部访问',
        },
        {
          key: 'serverName',
          label: '服务名称',
          type: 'text',
          default: 'kibana',
        },
        {
          key: 'serverBasePath',
          label: '基础路径',
          type: 'text',
          default: '',
          tip: '反向代理路径前缀，如 /kibana',
        },
        {
          key: 'locale',
          label: '界面语言',
          type: 'text',
          default: 'zh-CN',
          tip: '语言代码，如 zh-CN、en',
        },
      ],
    },
    {
      title: 'Elasticsearch 连接',
      fields: [
        {
          key: 'elasticsearchHosts',
          label: 'Elasticsearch 地址',
          type: 'text',
          default: 'http://localhost:9200',
          tip: '多个地址用逗号分隔',
        },
        {
          key: 'elasticsearchUsername',
          label: 'ES 用户名',
          type: 'text',
          default: 'kibana_system',
        },
        {
          key: 'elasticsearchPassword',
          label: 'ES 密码',
          type: 'text',
          default: 'changeme',
          tip: '生产环境务必修改',
        },
        {
          key: 'kibanaIndex',
          label: 'Kibana 索引',
          type: 'text',
          default: '.kibana',
        },
        {
          key: 'defaultAppId',
          label: '默认应用',
          type: 'text',
          default: 'discover',
          tip: '登录后默认打开的页面',
        },
      ],
    },
    {
      title: '安全配置',
      fields: [
        {
          key: 'enableSsl',
          label: '启用 SSL',
          type: 'switch',
          default: false,
          tip: '启用 HTTPS 访问',
        },
        {
          key: 'sslCert',
          label: 'SSL 证书文件',
          type: 'text',
          default: '/etc/kibana/cert.pem',
          dependsOn: { key: 'enableSsl', value: true },
        },
        {
          key: 'sslKey',
          label: 'SSL 私钥文件',
          type: 'text',
          default: '/etc/kibana/key.pem',
          dependsOn: { key: 'enableSsl', value: true },
        },
        {
          key: 'xpackSecurityEnabled',
          label: '启用 X-Pack 安全',
          type: 'switch',
          default: false,
        },
        {
          key: 'xpackEncryptedSavedObjectsEncryptionKey',
          label: '加密密钥',
          type: 'text',
          default: '',
          placeholder: '至少 32 位随机字符串',
          tip: '用于加密保存的对象',
          dependsOn: { key: 'xpackSecurityEnabled', value: true },
        },
      ],
    },
    {
      title: '日志配置',
      fields: [
        {
          key: 'loggingDest',
          label: '日志文件路径',
          type: 'text',
          default: '/var/log/kibana/kibana.log',
        },
        {
          key: 'loggingSilent',
          label: '静默模式',
          type: 'switch',
          default: false,
          tip: '仅输出致命错误',
        },
      ],
    },
  ],
}

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

  // Server
  lines.push(`# ======================== 服务配置 ========================`)
  lines.push(`server.port: ${config.serverPort}`)
  lines.push(`server.host: "${config.serverHost}"`)
  lines.push(`server.name: "${config.serverName}"`)
  if (config.serverBasePath) {
    lines.push(`server.basePath: "${config.serverBasePath}"`)
  }
  lines.push(`i18n.locale: "${config.locale}"`)
  lines.push(``)

  // Elasticsearch
  lines.push(`# ======================== Elasticsearch ========================`)
  lines.push(`elasticsearch.hosts: [${config.elasticsearchHosts.split(',').map(h => `"${h.trim()}"`).join(', ')}]`)
  lines.push(`elasticsearch.username: "${config.elasticsearchUsername}"`)
  lines.push(`elasticsearch.password: "${config.elasticsearchPassword}"`)
  lines.push(`kibana.index: "${config.kibanaIndex}"`)
  lines.push(`defaultAppId: "${config.defaultAppId}"`)
  lines.push(``)

  // Security
  lines.push(`# ======================== 安全配置 ========================`)
  if (config.enableSsl) {
    lines.push(`server.ssl.enabled: true`)
    lines.push(`server.ssl.certificate: ${config.sslCert}`)
    lines.push(`server.ssl.key: ${config.sslKey}`)
  }
  if (config.xpackSecurityEnabled) {
    lines.push(`xpack.security.enabled: true`)
    if (config.xpackEncryptedSavedObjectsEncryptionKey) {
      lines.push(`xpack.encryptedSavedObjects.encryptionKey: "${config.xpackEncryptedSavedObjectsEncryptionKey}"`)
    }
  }
  lines.push(``)

  // Logging
  lines.push(`# ======================== 日志配置 ========================`)
  lines.push(`logging.dest: ${config.loggingDest}`)
  lines.push(`logging.silent: ${config.loggingSilent}`)

  return lines.join('\n')
}
