export const logstashSchema = {
  id: 'logstash',
  name: 'Logstash',
  icon: 'Document',
  category: '日志监控',
  description: '日志收集、转换与转发的开源数据处理引擎',
  format: 'conf',
  fileName: 'logstash.conf',
  groups: [
    {
      title: '输入配置',
      fields: [
        {
          key: 'inputType',
          label: '输入类型',
          type: 'select',
          options: [
            { label: 'Beats - Filebeat/Winlogbeat 等 (推荐)', value: 'beats' },
            { label: 'File - 文件读取', value: 'file' },
            { label: 'Syslog - 系统日志协议', value: 'syslog' },
            { label: 'TCP - TCP 端口监听', value: 'tcp' },
            { label: 'UDP - UDP 端口监听', value: 'udp' },
            { label: 'Kafka - Kafka 消费', value: 'kafka' },
          ],
          default: 'beats',
        },
        {
          key: 'inputPort',
          label: '监听端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 5044,
          dependsOn: { key: 'inputType', valueIn: ['beats', 'tcp', 'udp'] },
        },
        {
          key: 'inputPath',
          label: '文件路径',
          type: 'text',
          default: '/var/log/*.log',
          tip: '支持通配符，如 /var/log/*.log',
          dependsOn: { key: 'inputType', value: 'file' },
        },
        {
          key: 'inputTopics',
          label: 'Kafka Topics',
          type: 'text',
          default: 'my-topic',
          dependsOn: { key: 'inputType', value: 'kafka' },
        },
        {
          key: 'inputKafkaBootstrap',
          label: 'Kafka Bootstrap Servers',
          type: 'text',
          default: 'localhost:9092',
          dependsOn: { key: 'inputType', value: 'kafka' },
        },
      ],
    },
    {
      title: '过滤配置',
      fields: [
        {
          key: 'filterGrok',
          label: '启用 Grok 解析',
          type: 'switch',
          default: true,
          tip: '使用 Grok 模式解析非结构化日志',
        },
        {
          key: 'grokPattern',
          label: 'Grok 模式',
          type: 'text',
          default: '%{COMBINEDAPACHELOG}',
          tip: '常用: COMBINEDAPACHELOG, SYSLOGLINE, NGINXACCESS',
          dependsOn: { key: 'filterGrok', value: true },
        },
        {
          key: 'filterGeoip',
          label: '启用 GeoIP 解析',
          type: 'switch',
          default: false,
          tip: '根据 IP 地址解析地理位置信息',
        },
        {
          key: 'geoipSource',
          label: 'GeoIP 来源字段',
          type: 'text',
          default: 'client_ip',
          dependsOn: { key: 'filterGeoip', value: true },
        },
        {
          key: 'filterMutate',
          label: '启用 Mutate 过滤',
          type: 'switch',
          default: false,
          tip: '字段重命名、删除、替换等操作',
        },
        {
          key: 'mutateRemoveFields',
          label: '移除字段',
          type: 'text',
          default: 'message,@version',
          tip: '逗号分隔要删除的字段',
          dependsOn: { key: 'filterMutate', value: true },
        },
      ],
    },
    {
      title: '输出配置',
      fields: [
        {
          key: 'outputType',
          label: '输出类型',
          type: 'select',
          options: [
            { label: 'Elasticsearch (推荐)', value: 'elasticsearch' },
            { label: 'File - 输出到文件', value: 'file' },
            { label: 'Stdout - 标准输出 (调试)', value: 'stdout' },
            { label: 'Kafka - 发送到 Kafka', value: 'kafka' },
          ],
          default: 'elasticsearch',
        },
        {
          key: 'outputHosts',
          label: '目标地址',
          type: 'text',
          default: 'http://localhost:9200',
          tip: '多个地址用逗号分隔',
        },
        {
          key: 'outputIndex',
          label: '索引名称',
          type: 'text',
          default: 'logstash-%{+YYYY.MM.dd}',
          dependsOn: { key: 'outputType', value: 'elasticsearch' },
        },
        {
          key: 'outputFilePath',
          label: '输出文件路径',
          type: 'text',
          default: '/var/log/output.log',
          dependsOn: { key: 'outputType', value: 'file' },
        },
      ],
    },
    {
      title: '管道性能',
      fields: [
        {
          key: 'pipelineWorkers',
          label: '管道工作线程数',
          type: 'number',
          min: 1,
          max: 16,
          default: 4,
          tip: '建议设置为 CPU 核心数',
        },
        {
          key: 'pipelineBatchSize',
          label: '批处理大小',
          type: 'number',
          min: 1,
          max: 5000,
          default: 125,
          tip: '每次处理的事件数量',
        },
      ],
    },
  ],
}

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

  // Input
  lines.push(`# ======================== 输入 ========================`)
  lines.push(`input {`)
  if (config.inputType === 'beats') {
    lines.push(`  beats {`)
    lines.push(`    port => ${config.inputPort}`)
    lines.push(`  }`)
  } else if (config.inputType === 'file') {
    lines.push(`  file {`)
    lines.push(`    path => "${config.inputPath}"`)
    lines.push(`    start_position => "beginning"`)
    lines.push(`    sincedb_path => "/dev/null"`)
    lines.push(`  }`)
  } else if (config.inputType === 'syslog') {
    lines.push(`  syslog {`)
    lines.push(`    port => 514`)
    lines.push(`    type => "syslog"`)
    lines.push(`  }`)
  } else if (config.inputType === 'tcp') {
    lines.push(`  tcp {`)
    lines.push(`    port => ${config.inputPort}`)
    lines.push(`    codec => json_lines`)
    lines.push(`  }`)
  } else if (config.inputType === 'udp') {
    lines.push(`  udp {`)
    lines.push(`    port => ${config.inputPort}`)
    lines.push(`    codec => json_lines`)
    lines.push(`  }`)
  } else if (config.inputType === 'kafka') {
    lines.push(`  kafka {`)
    lines.push(`    bootstrap_servers => "${config.inputKafkaBootstrap}"`)
    lines.push(`    topics => ["${config.inputTopics}"]`)
    lines.push(`    group_id => "logstash-group"`)
    lines.push(`  }`)
  }
  lines.push(`}`)
  lines.push(``)

  // Filter
  lines.push(`# ======================== 过滤 ========================`)
  lines.push(`filter {`)
  if (config.filterGrok) {
    lines.push(`  grok {`)
    lines.push(`    match => { "message" => "${config.grokPattern}" }`)
    lines.push(`  }`)
  }
  if (config.filterGeoip) {
    lines.push(`  geoip {`)
    lines.push(`    source => "${config.geoipSource}"`)
    lines.push(`  }`)
  }
  if (config.filterMutate) {
    lines.push(`  mutate {`)
    const fields = config.mutateRemoveFields.split(',').map(f => `"${f.trim()}"`).join(', ')
    lines.push(`    remove_field => [${fields}]`)
    lines.push(`  }`)
  }
  lines.push(`}`)
  lines.push(``)

  // Output
  lines.push(`# ======================== 输出 ========================`)
  lines.push(`output {`)
  if (config.outputType === 'elasticsearch') {
    lines.push(`  elasticsearch {`)
    lines.push(`    hosts => ["${config.outputHosts}"]`)
    lines.push(`    index => "${config.outputIndex}"`)
    lines.push(`  }`)
  } else if (config.outputType === 'file') {
    lines.push(`  file {`)
    lines.push(`    path => "${config.outputFilePath}"`)
    lines.push(`  }`)
  } else if (config.outputType === 'stdout') {
    lines.push(`  stdout {`)
    lines.push(`    codec => rubydebug`)
    lines.push(`  }`)
  } else if (config.outputType === 'kafka') {
    lines.push(`  kafka {`)
    lines.push(`    bootstrap_servers => "${config.outputHosts}"`)
    lines.push(`    topic_id => "logstash-output"`)
    lines.push(`  }`)
  }
  lines.push(`}`)
  lines.push(``)

  // Pipeline settings
  lines.push(`# ======================== 管道设置 ========================`)
  lines.push(`# 在 config/jvm.options 或启动参数中设置:`)
  lines.push(`# -Dpipeline.workers=${config.pipelineWorkers}`)
  lines.push(`# -Dpipeline.batch.size=${config.pipelineBatchSize}`)

  return lines.join('\n')
}
