export const etcdSchema = {
  id: 'etcd',
  name: 'Etcd',
  icon: 'Connection',
  category: '分布式协调',
  description: '分布式键值存储，用于配置共享和服务发现',
  format: 'yml',
  fileName: 'etcd.conf.yml',
  groups: [
    {
      title: '节点配置',
      fields: [
        {
          key: 'name',
          label: '节点名称',
          type: 'text',
          default: 'etcd-0',
          tip: '集群中每个节点的唯一名称',
        },
        {
          key: 'dataDir',
          label: '数据目录',
          type: 'text',
          default: '/var/lib/etcd',
        },
      ],
    },
    {
      title: '客户端通信',
      fields: [
        {
          key: 'listenClientUrls',
          label: '监听客户端地址',
          type: 'text',
          default: 'http://0.0.0.0:2379',
          tip: '客户端连接地址，多个用逗号分隔',
        },
        {
          key: 'advertiseClientUrls',
          label: '对外通告客户端地址',
          type: 'text',
          default: 'http://localhost:2379',
          tip: '通知客户端使用的地址',
        },
      ],
    },
    {
      title: '节点间通信',
      fields: [
        {
          key: 'listenPeerUrls',
          label: '监听节点间地址',
          type: 'text',
          default: 'http://0.0.0.0:2380',
          tip: '集群内部通信地址',
        },
        {
          key: 'initialAdvertisePeerUrls',
          label: '对外通告节点间地址',
          type: 'text',
          default: 'http://localhost:2380',
        },
        {
          key: 'initialCluster',
          label: '初始集群配置',
          type: 'text',
          default: 'etcd-0=http://localhost:2380',
          placeholder: 'node1=http://ip1:2380,node2=http://ip2:2380',
          tip: '格式: name1=url1,name2=url2',
        },
        {
          key: 'initialClusterState',
          label: '初始集群状态',
          type: 'select',
          options: [
            { label: 'new (新建集群)', value: 'new' },
            { label: 'existing (加入已有集群)', value: 'existing' },
          ],
          default: 'new',
        },
        {
          key: 'initialClusterToken',
          label: '集群令牌',
          type: 'text',
          default: 'etcd-cluster',
          tip: '防止集群间误加入',
        },
      ],
    },
    {
      title: 'TLS 安全',
      fields: [
        {
          key: 'enableTls',
          label: '启用 TLS',
          type: 'switch',
          default: false,
          tip: '生产环境建议开启',
        },
        {
          key: 'certFile',
          label: '证书文件路径',
          type: 'text',
          default: '/etc/etcd/server.crt',
          dependsOn: { key: 'enableTls', value: true },
        },
        {
          key: 'keyFile',
          label: '私钥文件路径',
          type: 'text',
          default: '/etc/etcd/server.key',
          dependsOn: { key: 'enableTls', value: true },
        },
      ],
    },
    {
      title: '压缩与存储',
      fields: [
        {
          key: 'autoCompactionMode',
          label: '自动压缩模式',
          type: 'select',
          options: [
            { label: 'periodic (按时间)', value: 'periodic' },
            { label: 'revision (按修订版本)', value: 'revision' },
          ],
          default: 'periodic',
          tip: '定期压缩历史数据以释放空间',
        },
        {
          key: 'autoCompactionRetention',
          label: '自动压缩保留',
          type: 'text',
          default: '1h',
          tip: 'periodic 模式: 1h/1d; revision 模式: 版本数',
        },
        {
          key: 'quotaBackendBytes',
          label: '后端存储配额 (字节)',
          type: 'number',
          min: 0,
          max: 8589934592,
          default: 8589934592,
          tip: '8GB，0 表示不限制',
        },
        {
          key: 'maxRequestBytes',
          label: '最大请求大小 (字节)',
          type: 'number',
          min: 1048576,
          max: 15728640,
          default: 1572864,
          tip: '1.5MB',
        },
      ],
    },
    {
      title: '性能与调试',
      fields: [
        {
          key: 'heartbeatInterval',
          label: '心跳间隔 (毫秒)',
          type: 'number',
          min: 50,
          max: 3000,
          default: 100,
          tip: '建议为 electionTimeout 的 1/5~1/10',
        },
        {
          key: 'electionTimeout',
          label: '选举超时 (毫秒)',
          type: 'number',
          min: 500,
          max: 30000,
          default: 1000,
          tip: '建议为 heartbeatInterval 的 5~10 倍',
        },
        {
          key: 'enablePprof',
          label: '开启 pprof',
          type: 'switch',
          default: false,
          tip: '调试时开启，生产环境建议关闭',
        },
      ],
    },
  ],
}

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

  // 节点配置
  lines.push(`# 节点配置`)
  lines.push(`name: '${config.name}'`)
  lines.push(`data-dir: '${config.dataDir}'`)
  lines.push(``)

  // 客户端通信
  lines.push(`# 客户端通信`)
  const listenClientUrls = config.listenClientUrls.split(',').map((u) => u.trim())
  lines.push(`listen-client-urls: '${listenClientUrls.join(',')}'`)
  const advertiseClientUrls = config.advertiseClientUrls.split(',').map((u) => u.trim())
  lines.push(`advertise-client-urls: '${advertiseClientUrls.join(',')}'`)
  lines.push(``)

  // 节点间通信
  lines.push(`# 节点间通信`)
  const listenPeerUrls = config.listenPeerUrls.split(',').map((u) => u.trim())
  lines.push(`listen-peer-urls: '${listenPeerUrls.join(',')}'`)
  const initialAdvertisePeerUrls = config.initialAdvertisePeerUrls.split(',').map((u) => u.trim())
  lines.push(`initial-advertise-peer-urls: '${initialAdvertisePeerUrls.join(',')}'`)
  lines.push(`initial-cluster: '${config.initialCluster}'`)
  lines.push(`initial-cluster-state: '${config.initialClusterState}'`)
  lines.push(`initial-cluster-token: '${config.initialClusterToken}'`)
  lines.push(``)

  // TLS 安全
  if (config.enableTls) {
    lines.push(`# TLS 安全`)
    lines.push(`client-transport-security:`)
    lines.push(`  cert-file: '${config.certFile}'`)
    lines.push(`  key-file: '${config.keyFile}'`)
    lines.push(`  client-cert-auth: true`)
    lines.push(`  trusted-ca-file: '/etc/etcd/ca.crt'`)
    lines.push(`peer-transport-security:`)
    lines.push(`  cert-file: '${config.certFile}'`)
    lines.push(`  key-file: '${config.keyFile}'`)
    lines.push(`  client-cert-auth: true`)
    lines.push(`  trusted-ca-file: '/etc/etcd/ca.crt'`)
    lines.push(``)
  }

  // 压缩与存储
  lines.push(`# 压缩与存储`)
  lines.push(`auto-compaction-mode: '${config.autoCompactionMode}'`)
  lines.push(`auto-compaction-retention: '${config.autoCompactionRetention}'`)
  lines.push(`quota-backend-bytes: ${config.quotaBackendBytes}`)
  lines.push(`max-request-bytes: ${config.maxRequestBytes}`)
  lines.push(``)

  // 性能与调试
  lines.push(`# 性能与调试`)
  lines.push(`heartbeat-interval: ${config.heartbeatInterval}`)
  lines.push(`election-timeout: ${config.electionTimeout}`)
  lines.push(`enable-pprof: ${config.enablePprof}`)
  lines.push(`logger: 'zap'`)
  lines.push(`log-outputs: ['stderr']`)

  return lines.join('\n')
}
