export const syslogSchema = {
  id: 'syslog',
  name: 'Syslog',
  icon: 'Document',
  category: '日志监控',
  description: 'RSyslog 系统日志收集与转发服务',
  format: 'conf',
  fileName: 'rsyslog.conf',
  groups: [
    {
      title: '接收协议',
      fields: [
        {
          key: 'enableUdp',
          label: '启用 UDP 接收',
          type: 'switch',
          default: true,
        },
        {
          key: 'udpPort',
          label: 'UDP 端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 514,
          dependsOn: { key: 'enableUdp', value: true },
        },
        {
          key: 'enableTcp',
          label: '启用 TCP 接收',
          type: 'switch',
          default: true,
        },
        {
          key: 'tcpPort',
          label: 'TCP 端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 514,
          dependsOn: { key: 'enableTcp', value: true },
        },
        {
          key: 'enableRelp',
          label: '启用 RELP 接收',
          type: 'switch',
          default: false,
          tip: '可靠事件日志协议，保证日志不丢失',
        },
        {
          key: 'relpPort',
          label: 'RELP 端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 2514,
          dependsOn: { key: 'enableRelp', value: true },
        },
      ],
    },
    {
      title: '日志格式',
      fields: [
        {
          key: 'logFormat',
          label: '日志格式',
          type: 'select',
          options: [
            { label: 'traditional - 传统格式', value: 'traditional' },
            { label: 'RFC5424 - 标准格式', value: 'RFC5424' },
            { label: 'RSYSLOG_FileFormat - 文件格式 (推荐)', value: 'RSYSLOG_FileFormat' },
          ],
          default: 'RSYSLOG_FileFormat',
        },
        {
          key: 'enableHighPrecisionTimestamps',
          label: '高精度时间戳',
          type: 'switch',
          default: true,
          tip: '微秒级时间戳',
        },
      ],
    },
    {
      title: '远程转发',
      fields: [
        {
          key: 'enableRemoteLogging',
          label: '启用远程转发',
          type: 'switch',
          default: false,
          tip: '将日志转发到远程服务器',
        },
        {
          key: 'remoteHost',
          label: '远程主机',
          type: 'text',
          default: '192.168.1.100',
          dependsOn: { key: 'enableRemoteLogging', value: true },
        },
        {
          key: 'remotePort',
          label: '远程端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 514,
          dependsOn: { key: 'enableRemoteLogging', value: true },
        },
        {
          key: 'remoteProtocol',
          label: '远程协议',
          type: 'select',
          options: [
            { label: 'udp', value: 'udp' },
            { label: 'tcp', value: 'tcp' },
            { label: 'relp', value: 'relp' },
          ],
          default: 'tcp',
          dependsOn: { key: 'enableRemoteLogging', value: true },
        },
      ],
    },
    {
      title: '文件输出',
      fields: [
        {
          key: 'enableFileOutput',
          label: '启用文件输出',
          type: 'switch',
          default: true,
        },
        {
          key: 'logDir',
          label: '日志目录',
          type: 'text',
          default: '/var/log',
          dependsOn: { key: 'enableFileOutput', value: true },
        },
        {
          key: 'enablePerHostDir',
          label: '按主机分目录',
          type: 'switch',
          default: false,
          tip: '按主机名创建独立日志目录',
          dependsOn: { key: 'enableFileOutput', value: true },
        },
      ],
    },
    {
      title: '速率限制',
      fields: [
        {
          key: 'enableRateLimiting',
          label: '启用速率限制',
          type: 'switch',
          default: true,
          tip: '防止日志风暴',
        },
        {
          key: 'rateLimitInterval',
          label: '限制间隔 (秒)',
          type: 'number',
          min: 0,
          max: 3600,
          default: 0,
          tip: '0=使用默认值',
          dependsOn: { key: 'enableRateLimiting', value: true },
        },
        {
          key: 'rateLimitBurst',
          label: '突发上限',
          type: 'number',
          min: 1,
          max: 1000000,
          default: 10000,
          dependsOn: { key: 'enableRateLimiting', value: true },
        },
      ],
    },
    {
      title: '日志源模块',
      fields: [
        {
          key: 'enableKernelLog',
          label: '内核日志',
          type: 'switch',
          default: true,
        },
        {
          key: 'enableMailLog',
          label: '邮件日志',
          type: 'switch',
          default: true,
        },
        {
          key: 'enableAuthLog',
          label: '认证日志',
          type: 'switch',
          default: true,
        },
      ],
    },
  ],
}

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

  // Modules
  lines.push(`# ======================== 模块加载 ========================`)
  if (config.enableUdp) {
    lines.push(`module(load="imudp")`)
  }
  if (config.enableTcp) {
    lines.push(`module(load="imtcp")`)
  }
  if (config.enableRelp) {
    lines.push(`module(load="imrelp")`)
  }
  if (config.enableKernelLog) {
    lines.push(`module(load="imklog")`)
  }
  lines.push(``)

  // Global directives
  lines.push(`# ======================== 全局设置 ========================`)
  lines.push(`$WorkDirectory /var/lib/rsyslog`)
  lines.push(`$ActionFileDefaultTemplate ${config.logFormat}`)
  if (config.enableHighPrecisionTimestamps) {
    lines.push(`$ActionFileDefaultTemplate RSYSLOG_FileFormat`)
  }
  lines.push(``)

  // Input listeners
  lines.push(`# ======================== 接收配置 ========================`)
  if (config.enableUdp) {
    lines.push(`input(type="imudp" port="${config.udpPort}" ruleset="remote")`)
  }
  if (config.enableTcp) {
    lines.push(`input(type="imtcp" port="${config.tcpPort}" ruleset="remote")`)
  }
  if (config.enableRelp) {
    lines.push(`input(type="imrelp" port="${config.relpPort}" ruleset="remote")`)
  }
  lines.push(``)

  // Rate limiting
  if (config.enableRateLimiting) {
    lines.push(`# ======================== 速率限制 ========================`)
    if (config.rateLimitInterval > 0) {
      lines.push(`$SystemLogRateLimitInterval ${config.rateLimitInterval}`)
    }
    lines.push(`$SystemLogRateLimitBurst ${config.rateLimitBurst}`)
    lines.push(``)
  }

  // Log sources
  lines.push(`# ======================== 日志源 ========================`)
  lines.push(`# 系统默认日志`)
  lines.push(`auth,authpriv.*                 ${config.enableFileOutput ? `${config.logDir}/auth.log` : '/dev/null'}`)
  lines.push(`*.*;auth,authpriv.none          ${config.enableFileOutput ? `${config.logDir}/syslog` : '/dev/null'}`)
  if (config.enableKernelLog) {
    lines.push(`kern.*                          ${config.enableFileOutput ? `${config.logDir}/kern.log` : '/dev/null'}`)
  }
  if (config.enableMailLog) {
    lines.push(`mail.*                          ${config.enableFileOutput ? `${config.logDir}/mail.log` : '/dev/null'}`)
  }
  lines.push(`mail.err                        ${config.enableFileOutput ? `${config.logDir}/mail.err` : '/dev/null'}`)
  lines.push(`cron.*                          ${config.enableFileOutput ? `${config.logDir}/cron.log` : '/dev/null'}`)
  lines.push(``)

  // File output
  if (config.enableFileOutput) {
    lines.push(`# ======================== 文件输出模板 ========================`)
    if (config.enablePerHostDir) {
      lines.push(`template(name="perHostFile" type="string"`)
      lines.push(`  string="${config.logDir}/%HOSTNAME%/%PROGRAMNAME%.log")`)
      lines.push(``)
      lines.push(`ruleset(name="remote") {`)
      lines.push(`  action(type="omfile" dynaFile="perHostFile")`)
      lines.push(`}`)
    } else {
      lines.push(`ruleset(name="remote") {`)
      lines.push(`  action(type="omfile" file="${config.logDir}/remote.log")`)
      lines.push(`}`)
    }
    lines.push(``)
  }

  // Remote forwarding
  if (config.enableRemoteLogging) {
    lines.push(`# ======================== 远程转发 ========================`)
    if (config.remoteProtocol === 'relp') {
      lines.push(`action(type="omrelp" target="${config.remoteHost}" port="${config.remotePort}")`)
    } else if (config.remoteProtocol === 'tcp') {
      lines.push(`action(type="omfwd" target="${config.remoteHost}" port="${config.remotePort}" protocol="tcp"`)
      lines.push(`  queue.type="linkedList" queue.filename="fwd_queue" queue.saveOnShutdown="on"`)
      lines.push(`  action.resumeRetryCount="-1")`)
    } else {
      lines.push(`action(type="omfwd" target="${config.remoteHost}" port="${config.remotePort}" protocol="udp")`)
    }
  }

  return lines.join('\n')
}
