export const rocketmqSchema = {
  id: 'rocketmq',
  name: 'RocketMQ',
  icon: 'ChatDotRound',
  category: '消息队列',
  description: '阿里巴巴开源的分布式消息中间件',
  format: 'conf',
  fileName: 'broker.conf',
  groups: [
    {
      title: 'Broker 基础配置',
      fields: [
        {
          key: 'brokerName',
          label: 'Broker 名称',
          type: 'text',
          default: 'broker-a',
        },
        {
          key: 'brokerId',
          label: 'Broker ID',
          type: 'number',
          min: 0,
          max: 10,
          default: 0,
          tip: '0 表示 Master，大于 0 表示 Slave',
        },
        {
          key: 'listenPort',
          label: '监听端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 10911,
        },
        {
          key: 'namesrvAddr',
          label: 'NameServer 地址',
          type: 'text',
          default: '127.0.0.1:9876',
          tip: '多个用分号分隔',
        },
      ],
    },
    {
      title: '存储配置',
      fields: [
        {
          key: 'storePathRootDir',
          label: '存储根目录',
          type: 'text',
          default: '/home/rocketmq/store',
        },
        {
          key: 'mapedFileSizeCommitLog',
          label: 'CommitLog 文件大小',
          type: 'number',
          min: 1048576,
          max: 2147483647,
          default: 1073741824,
          tip: '1GB',
        },
        {
          key: 'deleteWhen',
          label: '过期文件删除时间',
          type: 'text',
          default: '04',
          tip: '凌晨4点清理',
        },
        {
          key: 'fileReservedTime',
          label: '文件保留时间',
          type: 'number',
          min: 1,
          max: 720,
          default: 72,
          tip: 'hours',
        },
      ],
    },
    {
      title: 'Topic 配置',
      fields: [
        {
          key: 'defaultTopicQueueNums',
          label: '默认 Topic 队列数',
          type: 'number',
          min: 1,
          max: 64,
          default: 8,
        },
        {
          key: 'autoCreateTopicEnable',
          label: '自动创建 Topic',
          type: 'switch',
          default: true,
          tip: '生产建议关闭',
        },
        {
          key: 'autoCreateSubscriptionGroup',
          label: '自动创建订阅组',
          type: 'switch',
          default: true,
        },
      ],
    },
    {
      title: '高可用配置',
      fields: [
        {
          key: 'brokerRole',
          label: 'Broker 角色',
          type: 'select',
          options: [
            { label: 'ASYNC_MASTER - 异步复制 (默认)', value: 'ASYNC_MASTER' },
            { label: 'SYNC_MASTER - 同步双写', value: 'SYNC_MASTER' },
            { label: 'SLAVE - 从节点', value: 'SLAVE' },
          ],
          default: 'ASYNC_MASTER',
        },
        {
          key: 'flushDiskType',
          label: '刷盘策略',
          type: 'select',
          options: [
            { label: 'ASYNC_FLUSH - 异步刷盘 (性能高)', value: 'ASYNC_FLUSH' },
            { label: 'SYNC_FLUSH - 同步刷盘 (数据安全)', value: 'SYNC_FLUSH' },
          ],
          default: 'ASYNC_FLUSH',
        },
      ],
    },
    {
      title: '消息配置',
      fields: [
        {
          key: 'maxMessageSize',
          label: '最大消息大小',
          type: 'number',
          min: 1024,
          max: 16777216,
          default: 4194304,
          tip: '4MB',
        },
        {
          key: 'enablePropertyFilter',
          label: '开启属性过滤',
          type: 'switch',
          default: false,
          tip: '允许消费者使用 SQL92 过滤',
        },
      ],
    },
  ],
}

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

  lines.push(`# ======================== Broker 基础配置 ========================`)
  lines.push(`brokerName=${config.brokerName}`)
  lines.push(`brokerId=${config.brokerId}`)
  lines.push(`listenPort=${config.listenPort}`)
  lines.push(`namesrvAddr=${config.namesrvAddr}`)
  lines.push(``)

  lines.push(`# ======================== 存储配置 ========================`)
  lines.push(`storePathRootDir=${config.storePathRootDir}`)
  lines.push(`storePathCommitLog=${config.storePathRootDir}/commitlog`)
  lines.push(`storePathConsumeQueue=${config.storePathRootDir}/consumequeue`)
  lines.push(`storePathIndex=${config.storePathRootDir}/index`)
  lines.push(`mapedFileSizeCommitLog=${config.mapedFileSizeCommitLog}`)
  lines.push(`deleteWhen=${config.deleteWhen}`)
  lines.push(`fileReservedTime=${config.fileReservedTime}`)
  lines.push(``)

  lines.push(`# ======================== Topic 配置 ========================`)
  lines.push(`defaultTopicQueueNums=${config.defaultTopicQueueNums}`)
  lines.push(`autoCreateTopicEnable=${config.autoCreateTopicEnable}`)
  lines.push(`autoCreateSubscriptionGroup=${config.autoCreateSubscriptionGroup}`)
  lines.push(``)

  lines.push(`# ======================== 高可用配置 ========================`)
  lines.push(`brokerRole=${config.brokerRole}`)
  lines.push(`flushDiskType=${config.flushDiskType}`)
  lines.push(``)

  lines.push(`# ======================== 消息配置 ========================`)
  lines.push(`maxMessageSize=${config.maxMessageSize}`)
  lines.push(`enablePropertyFilter=${config.enablePropertyFilter}`)
  lines.push(``) 

  return lines.join('\n')
}
