export const postgresqlSchema = {
  id: 'postgresql',
  name: 'PostgreSQL',
  icon: 'Coin',
  category: '数据库',
  description: '功能强大的开源关系型数据库',
  format: 'conf',
  fileName: 'postgresql.conf',
  groups: [
    {
      title: '基础配置',
      fields: [
        {
          key: 'port',
          label: '监听端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 5432,
        },
        {
          key: 'maxConnections',
          label: '最大连接数',
          type: 'number',
          min: 10,
          max: 10000,
          default: 100,
        },
      ],
    },
    {
      title: '内存配置',
      fields: [
        {
          key: 'sharedBuffers',
          label: '共享缓冲区',
          type: 'select',
          options: [
            { label: '128MB', value: '128MB' },
            { label: '256MB (推荐)', value: '256MB' },
            { label: '512MB', value: '512MB' },
            { label: '1GB', value: '1GB' },
            { label: '2GB', value: '2GB' },
            { label: '4GB', value: '4GB' },
          ],
          default: '256MB',
          tip: '建议设为服务器内存的 25%',
        },
        {
          key: 'effectiveCacheSize',
          label: '有效缓存大小',
          type: 'select',
          options: [
            { label: '1GB', value: '1GB' },
            { label: '2GB', value: '2GB' },
            { label: '4GB', value: '4GB' },
            { label: '8GB', value: '8GB' },
            { label: '16GB', value: '16GB' },
          ],
          default: '1GB',
          tip: '建议设为服务器内存的 50-75%',
        },
        {
          key: 'workMem',
          label: '工作内存',
          type: 'select',
          options: [
            { label: '4MB', value: '4MB' },
            { label: '16MB', value: '16MB' },
            { label: '32MB', value: '32MB' },
            { label: '64MB', value: '64MB' },
          ],
          default: '4MB',
          tip: '每个排序/哈希操作使用的内存',
        },
        {
          key: 'maintenanceWorkMem',
          label: '维护工作内存',
          type: 'select',
          options: [
            { label: '64MB', value: '64MB' },
            { label: '128MB', value: '128MB' },
            { label: '256MB', value: '256MB' },
            { label: '512MB', value: '512MB' },
          ],
          default: '64MB',
          tip: '用于 VACUUM、CREATE INDEX 等操作',
        },
      ],
    },
    {
      title: 'WAL 配置',
      fields: [
        {
          key: 'walLevel',
          label: 'WAL 级别',
          type: 'select',
          options: [
            { label: 'replica - 支持复制和归档 (推荐)', value: 'replica' },
            { label: 'logical - 支持逻辑复制', value: 'logical' },
            { label: 'minimal - 最小化日志', value: 'minimal' },
          ],
          default: 'replica',
        },
        {
          key: 'maxWalSize',
          label: 'WAL 最大大小',
          type: 'select',
          options: [
            { label: '1GB', value: '1GB' },
            { label: '2GB', value: '2GB' },
            { label: '4GB', value: '4GB' },
          ],
          default: '1GB',
        },
        {
          key: 'checkpointCompletionTarget',
          label: 'Checkpoint 完成目标',
          type: 'number',
          min: 0,
          max: 1,
          step: 0.1,
          default: 0.9,
          tip: '0-1 之间，分散 checkpoint I/O',
        },
        {
          key: 'maxReplicationSlots',
          label: '最大复制槽数',
          type: 'number',
          min: 0,
          max: 10,
          default: 4,
        },
        {
          key: 'maxWalSenders',
          label: '最大 WAL 发送进程数',
          type: 'number',
          min: 0,
          max: 10,
          default: 3,
        },
      ],
    },
    {
      title: '日志配置',
      fields: [
        {
          key: 'logMinDurationStatement',
          label: '慢查询阈值',
          type: 'number',
          min: 0,
          max: 60000,
          default: 1000,
          tip: 'ms, 0=记录所有语句',
        },
      ],
    },
    {
      title: 'SSL 配置',
      fields: [
        {
          key: 'enableSsl',
          label: '开启 SSL',
          type: 'switch',
          default: false,
        },
        {
          key: 'sslCertFile',
          label: 'SSL 证书文件',
          type: 'text',
          default: '/etc/ssl/certs/server.crt',
          dependsOn: { key: 'enableSsl', value: true },
        },
        {
          key: 'sslKeyFile',
          label: 'SSL 私钥文件',
          type: 'text',
          default: '/etc/ssl/private/server.key',
          dependsOn: { key: 'enableSsl', value: true },
        },
      ],
    },
  ],
}

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

  lines.push(`# ======================== 连接配置 ========================`)
  lines.push(`listen_addresses = '*'`)
  lines.push(`port = ${config.port}`)
  lines.push(`max_connections = ${config.maxConnections}`)
  lines.push(``)

  lines.push(`# ======================== 内存配置 ========================`)
  lines.push(`shared_buffers = ${config.sharedBuffers}`)
  lines.push(`effective_cache_size = ${config.effectiveCacheSize}`)
  lines.push(`work_mem = ${config.workMem}`)
  lines.push(`maintenance_work_mem = ${config.maintenanceWorkMem}`)
  lines.push(``)

  lines.push(`# ======================== WAL 配置 ========================`)
  lines.push(`wal_level = ${config.walLevel}`)
  lines.push(`max_wal_size = ${config.maxWalSize}`)
  lines.push(`checkpoint_completion_target = ${config.checkpointCompletionTarget}`)
  lines.push(`max_replication_slots = ${config.maxReplicationSlots}`)
  lines.push(`max_wal_senders = ${config.maxWalSenders}`)
  lines.push(``)

  lines.push(`# ======================== 日志配置 ========================`)
  lines.push(`logging_collector = on`)
  lines.push(`log_directory = 'log'`)
  lines.push(`log_filename = 'postgresql-%Y-%m-%d.log'`)
  lines.push(`log_min_duration_statement = ${config.logMinDurationStatement}`)
  lines.push(``)

  if (config.enableSsl) {
    lines.push(`# ======================== SSL 配置 ========================`)
    lines.push(`ssl = on`)
    lines.push(`ssl_cert_file = '${config.sslCertFile}'`)
    lines.push(`ssl_key_file = '${config.sslKeyFile}'`)
    lines.push(``)
  }

  return lines.join('\n')
}
