export const apacheSchema = {
  id: 'apache',
  name: 'Apache',
  icon: 'Monitor',
  category: '代理与Web',
  description: '世界上使用最广泛的 HTTP Web 服务器',
  format: 'conf',
  fileName: 'httpd.conf',
  groups: [
    {
      title: '基础配置',
      fields: [
        {
          key: 'serverRoot',
          label: '服务器根目录',
          type: 'text',
          default: '/etc/httpd',
        },
        {
          key: 'listen',
          label: '监听端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 80,
        },
        {
          key: 'serverName',
          label: '服务器域名',
          type: 'text',
          placeholder: 'www.example.com:80',
          default: 'localhost:80',
          required: true,
        },
        {
          key: 'serverAdmin',
          label: '管理员邮箱',
          type: 'text',
          placeholder: 'admin@example.com',
          default: 'admin@localhost',
        },
        {
          key: 'documentRoot',
          label: '网站根目录',
          type: 'text',
          default: '/var/www/html',
        },
      ],
    },
    {
      title: 'MPM 模块',
      fields: [
        {
          key: 'mpmModule',
          label: 'MPM 模型',
          type: 'select',
          options: [
            { label: 'event - 高并发首选 (推荐)', value: 'event' },
            { label: 'worker - 线程池模型', value: 'worker' },
            { label: 'prefork - 进程模型 (兼容性最好)', value: 'prefork' },
          ],
          default: 'event',
          tip: 'event 适合高并发，prefork 兼容 mod_php',
        },
        {
          key: 'startServers',
          label: '启动进程数',
          type: 'number',
          min: 1,
          max: 64,
          default: 4,
        },
        {
          key: 'minSpareThreads',
          label: '最小空闲线程',
          type: 'number',
          min: 1,
          max: 256,
          default: 25,
          dependsOn: { key: 'mpmModule', valueNotIn: ['prefork'] },
        },
        {
          key: 'maxSpareThreads',
          label: '最大空闲线程',
          type: 'number',
          min: 1,
          max: 1024,
          default: 75,
          dependsOn: { key: 'mpmModule', valueNotIn: ['prefork'] },
        },
        {
          key: 'maxRequestWorkers',
          label: '最大并发数',
          type: 'number',
          min: 1,
          max: 10000,
          default: 256,
        },
        {
          key: 'threadsPerChild',
          label: '每进程线程数',
          type: 'number',
          min: 1,
          max: 128,
          default: 25,
          dependsOn: { key: 'mpmModule', valueNotIn: ['prefork'] },
        },
        {
          key: 'minSpareServers',
          label: '最小空闲进程',
          type: 'number',
          min: 1,
          max: 64,
          default: 5,
          dependsOn: { key: 'mpmModule', value: 'prefork' },
        },
        {
          key: 'maxSpareServers',
          label: '最大空闲进程',
          type: 'number',
          min: 1,
          max: 256,
          default: 10,
          dependsOn: { key: 'mpmModule', value: 'prefork' },
        },
      ],
    },
    {
      title: 'HTTPS / SSL',
      fields: [
        {
          key: 'enableSsl',
          label: '开启 HTTPS',
          type: 'switch',
          default: false,
        },
        {
          key: 'sslPort',
          label: 'HTTPS 端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 443,
          dependsOn: { key: 'enableSsl', value: true },
        },
        {
          key: 'sslCertificate',
          label: 'SSL 证书路径',
          type: 'text',
          default: '/etc/pki/tls/certs/localhost.crt',
          dependsOn: { key: 'enableSsl', value: true },
        },
        {
          key: 'sslCertificateKey',
          label: 'SSL 私钥路径',
          type: 'text',
          default: '/etc/pki/tls/private/localhost.key',
          dependsOn: { key: 'enableSsl', value: true },
        },
        {
          key: 'sslProtocols',
          label: 'TLS 版本',
          type: 'select',
          options: [
            { label: 'TLSv1.2 + TLSv1.3 (推荐)', value: 'all -SSLv3 -TLSv1 -TLSv1.1' },
            { label: 'TLSv1.1 + TLSv1.2 + TLSv1.3', value: 'all -SSLv3 -TLSv1' },
            { label: '仅 TLSv1.3', value: 'TLSv1.3' },
          ],
          default: 'all -SSLv3 -TLSv1 -TLSv1.1',
          dependsOn: { key: 'enableSsl', value: true },
        },
      ],
    },
    {
      title: '模块管理',
      fields: [
        {
          key: 'enableModRewrite',
          label: '加载 mod_rewrite',
          type: 'switch',
          default: true,
          tip: 'URL 重写模块',
        },
        {
          key: 'enableModProxy',
          label: '加载 mod_proxy',
          type: 'switch',
          default: false,
          tip: '反向代理模块',
        },
        {
          key: 'enableModDeflate',
          label: '加载 mod_deflate',
          type: 'switch',
          default: true,
          tip: '压缩传输模块',
        },
        {
          key: 'enableModExpires',
          label: '加载 mod_expires',
          type: 'switch',
          default: true,
          tip: '浏览器缓存控制',
        },
        {
          key: 'enableModSecurity',
          label: '加载 mod_security',
          type: 'switch',
          default: false,
          tip: 'Web 应用防火墙',
        },
      ],
    },
    {
      title: '反向代理',
      fields: [
        {
          key: 'enableProxy',
          label: '开启反向代理',
          type: 'switch',
          default: false,
        },
        {
          key: 'proxyTarget',
          label: '后端地址',
          type: 'text',
          placeholder: 'http://127.0.0.1:8080',
          default: 'http://127.0.0.1:8080',
          dependsOn: { key: 'enableProxy', value: true },
          required: true,
        },
        {
          key: 'proxyPath',
          label: '代理路径前缀',
          type: 'text',
          default: '/',
          dependsOn: { key: 'enableProxy', value: true },
        },
      ],
    },
    {
      title: '日志与性能',
      fields: [
        {
          key: 'logLevel',
          label: '日志级别',
          type: 'select',
          options: [
            { label: 'warn (推荐生产)', value: 'warn' },
            { label: 'error', value: 'error' },
            { label: 'info', value: 'info' },
            { label: 'debug', value: 'debug' },
          ],
          default: 'warn',
        },
        {
          key: 'enableGzip',
          label: '开启 Gzip 压缩',
          type: 'switch',
          default: true,
        },
        {
          key: 'keepAlive',
          label: '开启 Keep-Alive',
          type: 'switch',
          default: true,
        },
        {
          key: 'maxKeepAliveRequests',
          label: 'Keep-Alive 最大请求数',
          type: 'number',
          min: 0,
          max: 10000,
          default: 100,
          dependsOn: { key: 'keepAlive', value: true },
        },
        {
          key: 'keepAliveTimeout',
          label: 'Keep-Alive 超时 (秒)',
          type: 'number',
          min: 1,
          max: 300,
          default: 5,
          dependsOn: { key: 'keepAlive', value: true },
        },
        {
          key: 'timeout',
          label: '请求超时 (秒)',
          type: 'number',
          min: 10,
          max: 600,
          default: 60,
        },
        {
          key: 'serverTokens',
          label: '隐藏版本信息',
          type: 'switch',
          default: true,
          tip: '不在响应头暴露 Apache 版本',
        },
      ],
    },
  ],
}

export function generateApacheConf(config) {
  const lines = []
  lines.push(`# Apache HTTP Server 配置文件 - 由 ConfTemplate 生成`)
  lines.push(`# 生成时间: ${new Date().toLocaleString('zh-CN')}`)
  lines.push(``)
  lines.push(`ServerRoot "${config.serverRoot}"`)
  lines.push(`Listen ${config.listen}`)
  lines.push(`ServerName ${config.serverName}`)
  lines.push(`ServerAdmin ${config.serverAdmin}`)
  lines.push(`DocumentRoot "${config.documentRoot}"`)
  lines.push(``)

  if (config.serverTokens) {
    lines.push(`# 隐藏版本信息`)
    lines.push(`ServerTokens Prod`)
    lines.push(`ServerSignature Off`)
    lines.push(``)
  }

  // MPM
  lines.push(`# ======================== MPM 模块 ========================`)
  lines.push(`<IfModule mpm_${config.mpmModule}_module>`)

  if (config.mpmModule === 'prefork') {
    lines.push(`    StartServers          ${config.startServers}`)
    lines.push(`    MinSpareServers       ${config.minSpareServers}`)
    lines.push(`    MaxSpareServers       ${config.maxSpareServers}`)
    lines.push(`    MaxRequestWorkers     ${config.maxRequestWorkers}`)
    lines.push(`    MaxConnectionsPerChild 0`)
  } else {
    lines.push(`    StartServers          ${config.startServers}`)
    lines.push(`    MinSpareThreads       ${config.minSpareThreads}`)
    lines.push(`    MaxSpareThreads       ${config.maxSpareThreads}`)
    lines.push(`    ThreadsPerChild       ${config.threadsPerChild}`)
    lines.push(`    MaxRequestWorkers     ${config.maxRequestWorkers}`)
    lines.push(`    MaxConnectionsPerChild 0`)
  }

  lines.push(`</IfModule>`)
  lines.push(``)

  // 模块
  lines.push(`# ======================== 模块加载 ========================`)
  if (config.enableModRewrite) lines.push(`LoadModule rewrite_module modules/mod_rewrite.so`)
  if (config.enableModProxy) {
    lines.push(`LoadModule proxy_module modules/mod_proxy.so`)
    lines.push(`LoadModule proxy_http_module modules/mod_proxy_http.so`)
  }
  if (config.enableModDeflate) lines.push(`LoadModule deflate_module modules/mod_deflate.so`)
  if (config.enableModExpires) lines.push(`LoadModule expires_module modules/mod_expires.so`)
  if (config.enableModSecurity) lines.push(`LoadModule security2_module modules/mod_security2.so`)
  lines.push(`LoadModule dir_module modules/mod_dir.so`)
  lines.push(`LoadModule mime_module modules/mod_mime.so`)
  lines.push(`LoadModule log_config_module modules/mod_log_config.so`)
  lines.push(`LoadModule headers_module modules/mod_headers.so`)
  lines.push(``)

  // HTTPS
  if (config.enableSsl) {
    lines.push(`# ======================== HTTPS / SSL ========================`)
    lines.push(`LoadModule ssl_module modules/mod_ssl.so`)
    lines.push(``)
    lines.push(`<IfModule ssl_module>`)
    lines.push(`    SSLProtocol ${config.sslProtocols}`)
    lines.push(`    SSLCipherSuite HIGH:!aNULL:!MD5`)
    lines.push(`    SSLHonorCipherOrder on`)
    lines.push(`</IfModule>`)
    lines.push(``)
    lines.push(`<VirtualHost *:${config.sslPort}>`)
    lines.push(`    ServerName ${config.serverName}`)
    lines.push(`    SSLEngine on`)
    lines.push(`    SSLCertificateFile "${config.sslCertificate}"`)
    lines.push(`    SSLCertificateKeyFile "${config.sslCertificateKey}"`)
    lines.push(`    DocumentRoot "${config.documentRoot}"`)
    lines.push(`</VirtualHost>`)
    lines.push(``)
  }

  // Gzip
  if (config.enableGzip) {
    lines.push(`# ======================== Gzip 压缩 ========================`)
    lines.push(`<IfModule deflate_module>`)
    lines.push(`    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css`)
    lines.push(`    AddOutputFilterByType DEFLATE application/javascript application/json application/xml`)
    lines.push(`    DeflateCompressionLevel 6`)
    lines.push(`</IfModule>`)
    lines.push(``)
  }

  // 反向代理
  if (config.enableProxy) {
    lines.push(`# ======================== 反向代理 ========================`)
    lines.push(`<IfModule proxy_module>`)
    lines.push(`    ProxyPreserveHost On`)
    lines.push(`    ProxyPass ${config.proxyPath} ${config.proxyTarget}/`)
    lines.push(`    ProxyPassReverse ${config.proxyPath} ${config.proxyTarget}/`)
    lines.push(`</IfModule>`)
    lines.push(``)
  }

  // 日志
  lines.push(`# ======================== 日志配置 ========================`)
  lines.push(`LogLevel ${config.logLevel}`)
  lines.push(`ErrorLog "logs/error_log"`)
  lines.push(`CustomLog "logs/access_log" combined`)
  lines.push(``)

  // Keep-Alive
  lines.push(`# ======================== Keep-Alive ========================`)
  lines.push(`KeepAlive ${config.keepAlive ? 'On' : 'Off'}`)
  if (config.keepAlive) {
    lines.push(`MaxKeepAliveRequests ${config.maxKeepAliveRequests}`)
    lines.push(`KeepAliveTimeout ${config.keepAliveTimeout}`)
  }
  lines.push(`Timeout ${config.timeout}`)
  lines.push(``)

  // 目录权限
  lines.push(`# ======================== 目录权限 ========================`)
  lines.push(`<Directory "${config.documentRoot}">`)
  lines.push(`    Options FollowSymLinks`)
  lines.push(`    AllowOverride All`)
  lines.push(`    Require all granted`)
  lines.push(`</Directory>`)

  return lines.join('\n')
}
