export const mongodbSchema = {
  id: 'mongodb',
  name: 'MongoDB',
  icon: 'Coin',
  category: '数据库',
  description: '面向文档的 NoSQL 数据库',
  format: 'conf',
  fileName: 'mongod.conf',
  groups: [
    {
      title: '基础配置',
      fields: [
        {
          key: 'port',
          label: '监听端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 27017,
        },
        {
          key: 'bindIp',
          label: '绑定地址',
          type: 'text',
          default: '0.0.0.0',
          tip: '0.0.0.0 允许所有地址连接',
        },
        {
          key: 'dbPath',
          label: '数据目录',
          type: 'text',
          default: '/var/lib/mongodb',
        },
        {
          key: 'logPath',
          label: '日志文件路径',
          type: 'text',
          default: '/var/log/mongodb/mongod.log',
        },
        {
          key: 'fork',
          label: '守护进程模式',
          type: 'switch',
          default: true,
        },
      ],
    },
    {
      title: '安全认证',
      fields: [
        {
          key: 'auth',
          label: '开启身份认证',
          type: 'switch',
          default: true,
          tip: '生产环境必须开启',
        },
        {
          key: 'authorization',
          label: '授权模式',
          type: 'select',
          options: [
            { label: 'enabled - 基于角色的访问控制 (推荐)', value: 'enabled' },
            { label: 'disabled - 无访问控制', value: 'disabled' },
          ],
          default: 'enabled',
          dependsOn: { key: 'auth', value: true },
        },
        {
          key: 'enableTls',
          label: '开启 TLS/SSL',
          type: 'switch',
          default: false,
        },
        {
          key: 'tlsCertFile',
          label: 'TLS 证书文件',
          type: 'text',
          placeholder: '/etc/ssl/mongodb.pem',
          default: '/etc/ssl/mongodb.pem',
          dependsOn: { key: 'enableTls', value: true },
        },
        {
          key: 'tlsKeyFile',
          label: 'TLS 私钥文件',
          type: 'text',
          placeholder: '/etc/ssl/mongodb-key.pem',
          default: '/etc/ssl/mongodb-key.pem',
          dependsOn: { key: 'enableTls', value: true },
        },
      ],
    },
    {
      title: '存储引擎',
      fields: [
        {
          key: 'engine',
          label: '存储引擎',
          type: 'select',
          options: [
            { label: 'WiredTiger (默认，推荐)', value: 'wiredTiger' },
          ],
          default: 'wiredTiger',
        },
        {
          key: 'cacheSizeGB',
          label: 'WiredTiger 缓存大小 (GB)',
          type: 'select',
          options: [
            { label: '自动 (物理内存的 50%)', value: '' },
            { label: '1 GB', value: '1' },
            { label: '2 GB', value: '2' },
            { label: '4 GB', value: '4' },
            { label: '8 GB', value: '8' },
            { label: '16 GB', value: '16' },
          ],
          default: '',
          tip: '默认为 (物理内存 - 1GB) 的 50%',
        },
        {
          key: 'journalEnabled',
          label: '开启 Journal 日志',
          type: 'switch',
          default: true,
          tip: '保证数据持久性，单节点必须开启',
        },
        {
          key: 'directoryPerDb',
          label: '每个数据库独立目录',
          type: 'switch',
          default: false,
        },
      ],
    },
    {
      title: '网络与连接',
      fields: [
        {
          key: 'maxConns',
          label: '最大连接数',
          type: 'number',
          min: 10,
          max: 65536,
          default: 65536,
        },
        {
          key: 'ipv6',
          label: '启用 IPv6',
          type: 'switch',
          default: false,
        },
        {
          key: 'httpInterface',
          label: '开启 HTTP 接口',
          type: 'switch',
          default: false,
          tip: '生产环境建议关闭',
        },
      ],
    },
    {
      title: '操作日志 (Oplog)',
      fields: [
        {
          key: 'oplogSizeMB',
          label: 'Oplog 大小 (MB)',
          type: 'number',
          min: 0,
          max: 102400,
          step: 512,
          default: 0,
          tip: '0 表示使用默认值（磁盘的 5%）',
        },
        {
          key: 'replSetName',
          label: '副本集名称',
          type: 'text',
          placeholder: '留空表示不启用副本集',
          default: '',
          tip: '设置后启用副本集模式',
        },
      ],
    },
    {
      title: '性能调优',
      fields: [
        {
          key: 'wiredTigerCollectionBlockCompressor',
          label: '压缩算法',
          type: 'select',
          options: [
            { label: 'snappy (默认，平衡)', value: 'snappy' },
            { label: 'zlib (高压缩率)', value: 'zlib' },
            { label: 'zstd (最佳压缩)', value: 'zstd' },
            { label: 'none (不压缩)', value: 'none' },
          ],
          default: 'snappy',
        },
        {
          key: 'profileLevel',
          label: 'Profiling 级别',
          type: 'select',
          options: [
            { label: '0 - 关闭 (推荐生产)', value: '0' },
            { label: '1 - 慢操作', value: '1' },
            { label: '2 - 所有操作', value: '2' },
          ],
          default: '0',
          tip: '性能分析，调试时开启',
        },
        {
          key: 'slowOpThresholdMs',
          label: '慢操作阈值 (毫秒)',
          type: 'number',
          min: 0,
          max: 60000,
          default: 100,
          dependsOn: { key: 'profileLevel', valueNotIn: ['0'] },
        },
      ],
    },
  ],
}

export function generateMongodbConf(config) {
  const lines = []
  lines.push(`# MongoDB 配置文件 - 由 ConfTemplate 生成`)
  lines.push(`# 生成时间: ${new Date().toLocaleString('zh-CN')}`)
  lines.push(``)
  lines.push(`# ======================== 系统日志 ========================`)
  lines.push(`systemLog:`)
  lines.push(`  destination: file`)
  lines.push(`  path: ${config.logPath}`)
  lines.push(`  logAppend: true`)
  lines.push(``)
  lines.push(`# ======================== 存储 ========================`)
  lines.push(`storage:`)
  lines.push(`  dbPath: ${config.dbPath}`)
  lines.push(`  engine: ${config.engine}`)
  lines.push(`  directoryPerDB: ${config.directoryPerDb}`)

  if (config.journalEnabled) {
    lines.push(`  journal:`)
    lines.push(`    enabled: true`)
  }

  if (config.cacheSizeGB) {
    lines.push(`  wiredTiger:`)
    lines.push(`    engineConfig:`)
    lines.push(`      cacheSizeGB: ${config.cacheSizeGB}`)
    lines.push(`    collectionConfig:`)
    lines.push(`      blockCompressor: ${config.wiredTigerCollectionBlockCompressor}`)
  } else {
    lines.push(`  wiredTiger:`)
    lines.push(`    collectionConfig:`)
    lines.push(`      blockCompressor: ${config.wiredTigerCollectionBlockCompressor}`)
  }

  lines.push(``)
  lines.push(`# ======================== 网络 ========================`)
  lines.push(`net:`)
  lines.push(`  port: ${config.port}`)
  lines.push(`  bindIp: ${config.bindIp}`)
  lines.push(`  maxIncomingConnections: ${config.maxConns}`)

  if (config.ipv6) {
    lines.push(`  ipv6: true`)
  }

  if (config.httpInterface) {
    lines.push(`  http:`)
    lines.push(`    enabled: true`)
  }

  if (config.enableTls) {
    lines.push(`  tls:`)
    lines.push(`    mode: requireTLS`)
    lines.push(`    certificateKeyFile: ${config.tlsCertFile}`)
  }

  lines.push(``)
  lines.push(`# ======================== 安全 ========================`)
  lines.push(`security:`)

  if (config.auth) {
    lines.push(`  authorization: ${config.authorization}`)
  } else {
    lines.push(`  authorization: disabled`)
  }

  if (config.replSetName) {
    lines.push(``)
    lines.push(`# ======================== 副本集 ========================`)
    lines.push(`replication:`)
    lines.push(`  replSetName: ${config.replSetName}`)
    if (config.oplogSizeMB > 0) {
      lines.push(`  oplogSizeMB: ${config.oplogSizeMB}`)
    }
  }

  if (config.profileLevel !== '0') {
    lines.push(``)
    lines.push(`# ======================== 操作日志 ========================`)
    lines.push(`operationProfiling:`)
    lines.push(`  mode: ${parseInt(config.profileLevel) === 1 ? 'slowOp' : 'all'}`)
    lines.push(`  slowOpThresholdMs: ${config.slowOpThresholdMs}`)
  }

  lines.push(``)
  lines.push(`# ======================== 进程管理 ========================`)
  lines.push(`processManagement:`)
  lines.push(`  fork: ${config.fork}`)
  lines.push(`  pidFilePath: /var/run/mongodb/mongod.pid`)
  lines.push(`  timeZoneInfo: /usr/share/zoneinfo`)

  return lines.join('\n')
}
