export const kafkaSchema = {
  id: 'kafka',
  name: 'Kafka',
  icon: 'ChatLineRound',
  category: '消息队列',
  description: 'Apache Kafka 分布式流处理平台',
  format: 'properties',
  fileName: 'server.properties',
  groups: [
    {
      title: 'Broker 基础配置',
      fields: [
        {
          key: 'brokerId',
          label: 'Broker ID',
          type: 'number',
          min: 0,
          max: 10000,
          default: 0,
        },
        {
          key: 'listeners',
          label: '监听地址',
          type: 'text',
          default: 'PLAINTEXT://0.0.0.0:9092',
        },
        {
          key: 'advertisedListeners',
          label: '外部访问地址',
          type: 'text',
          default: 'PLAINTEXT://localhost:9092',
          tip: '外部访问地址',
        },
      ],
    },
    {
      title: 'Topic 与分区',
      fields: [
        {
          key: 'numPartitions',
          label: '默认分区数',
          type: 'number',
          min: 1,
          max: 100,
          default: 3,
        },
        {
          key: 'defaultReplicationFactor',
          label: '默认副本因子',
          type: 'number',
          min: 1,
          max: 10,
          default: 2,
        },
        {
          key: 'autoCreateTopicsEnable',
          label: '自动创建 Topic',
          type: 'switch',
          default: false,
          tip: '生产建议关闭',
        },
        {
          key: 'minInsyncReplicas',
          label: '最小同步副本数',
          type: 'number',
          min: 1,
          max: 10,
          default: 2,
          tip: '配合 acks=all 使用',
        },
      ],
    },
    {
      title: '日志存储',
      fields: [
        {
          key: 'logDirs',
          label: '日志存储目录',
          type: 'text',
          default: '/tmp/kafka-logs',
        },
        {
          key: 'logRetentionHours',
          label: '日志保留时间',
          type: 'number',
          min: 1,
          max: 720,
          default: 168,
          tip: '7天',
        },
        {
          key: 'logSegmentBytes',
          label: '日志段大小',
          type: 'number',
          min: 1048576,
          max: 2147483647,
          default: 1073741824,
          tip: '1GB',
        },
        {
          key: 'logRetentionBytes',
          label: '日志保留大小',
          type: 'number',
          min: -1,
          max: 9223372036854775807,
          default: -1,
          tip: '-1不限制',
        },
      ],
    },
    {
      title: '线程配置',
      fields: [
        {
          key: 'numIoThreads',
          label: 'IO 线程数',
          type: 'number',
          min: 1,
          max: 32,
          default: 8,
          tip: '处理网络请求的线程数',
        },
        {
          key: 'numNetworkThreads',
          label: '网络线程数',
          type: 'number',
          min: 1,
          max: 32,
          default: 3,
          tip: '处理网络请求的线程数',
        },
      ],
    },
    {
      title: '消息配置',
      fields: [
        {
          key: 'messageMaxBytes',
          label: '最大消息大小',
          type: 'number',
          min: 1024,
          max: 104857600,
          default: 1000012,
          tip: '~1MB',
        },
      ],
    },
    {
      title: 'Zookeeper',
      fields: [
        {
          key: 'zookeeperConnect',
          label: 'Zookeeper 地址',
          type: 'text',
          default: 'localhost:2181',
          tip: '多个用逗号分隔',
        },
        {
          key: 'offsetsRetentionMinutes',
          label: '偏移量保留时间',
          type: 'number',
          min: 1,
          max: 100800,
          default: 10080,
          tip: '7天',
        },
      ],
    },
    {
      title: '安全配置',
      fields: [
        {
          key: 'enableSsl',
          label: '开启 SSL',
          type: 'switch',
          default: false,
        },
      ],
    },
  ],
}

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

  lines.push(`# ======================== Broker 基础配置 ========================`)
  lines.push(`broker.id=${config.brokerId}`)
  lines.push(`listeners=${config.listeners}`)
  lines.push(`advertised.listeners=${config.advertisedListeners}`)
  lines.push(``)

  lines.push(`# ======================== Topic 与分区 ========================`)
  lines.push(`num.partitions=${config.numPartitions}`)
  lines.push(`default.replication.factor=${config.defaultReplicationFactor}`)
  lines.push(`auto.create.topics.enable=${config.autoCreateTopicsEnable}`)
  lines.push(`min.insync.replicas=${config.minInsyncReplicas}`)
  lines.push(``)

  lines.push(`# ======================== 日志存储 ========================`)
  lines.push(`log.dirs=${config.logDirs}`)
  lines.push(`log.retention.hours=${config.logRetentionHours}`)
  lines.push(`log.segment.bytes=${config.logSegmentBytes}`)
  lines.push(`log.retention.bytes=${config.logRetentionBytes}`)
  lines.push(`log.cleanup.policy=delete`)
  lines.push(`log.flush.interval.messages=10000`)
  lines.push(`log.flush.interval.ms=1000`)
  lines.push(``)

  lines.push(`# ======================== 线程配置 ========================`)
  lines.push(`num.io.threads=${config.numIoThreads}`)
  lines.push(`num.network.threads=${config.numNetworkThreads}`)
  lines.push(`num.replica.fetchers=1`)
  lines.push(`queued.max.requests=500`)
  lines.push(``)

  lines.push(`# ======================== 消息配置 ========================`)
  lines.push(`message.max.bytes=${config.messageMaxBytes}`)
  lines.push(`replica.fetch.max.bytes=${config.messageMaxBytes}`)
  lines.push(`offsets.topic.replication.factor=3`)
  lines.push(`transaction.state.log.replication.factor=3`)
  lines.push(`transaction.state.log.min.isr=2`)
  lines.push(``)

  lines.push(`# ======================== Zookeeper ========================`)
  lines.push(`zookeeper.connect=${config.zookeeperConnect}`)
  lines.push(`zookeeper.connection.timeout.ms=18000`)
  lines.push(`offsets.retention.minutes=${config.offsetsRetentionMinutes}`)
  lines.push(`group.initial.rebalance.delay.ms=3000`)
  lines.push(``)

  if (config.enableSsl) {
    lines.push(`# ======================== SSL 配置 ========================`)
    lines.push(`security.inter.broker.protocol=SSL`)
    lines.push(`ssl.keystore.location=/etc/kafka/ssl/kafka.keystore.jks`)
    lines.push(`ssl.keystore.password=changeit`)
    lines.push(`ssl.key.password=changeit`)
    lines.push(`ssl.truststore.location=/etc/kafka/ssl/kafka.truststore.jks`)
    lines.push(`ssl.truststore.password=changeit`)
    lines.push(`ssl.client.auth=required`)
    lines.push(``)
  }

  return lines.join('\n')
}
