export const bindSchema = {
  id: 'bind',
  name: 'BIND (DNS)',
  icon: 'Connection',
  category: '系统服务',
  description: 'BIND DNS 服务器配置',
  format: 'conf',
  fileName: 'named.conf',
  groups: [
    {
      title: '监听与查询',
      fields: [
        {
          key: 'listenOn',
          label: 'IPv4 监听地址',
          type: 'text',
          default: '127.0.0.1; any',
          tip: 'IPv4 监听地址列表',
        },
        {
          key: 'listenOnV6',
          label: 'IPv6 监听地址',
          type: 'text',
          default: '::1; any',
          tip: 'IPv6 监听地址列表',
        },
        {
          key: 'allowQuery',
          label: '允许查询',
          type: 'text',
          default: 'localhost; any',
          tip: '允许哪些客户端查询',
        },
        {
          key: 'allowRecursion',
          label: '允许递归',
          type: 'text',
          default: 'localhost; localnets',
          tip: '允许哪些客户端进行递归查询',
        },
        {
          key: 'recursion',
          label: '启用递归',
          type: 'switch',
          default: true,
        },
      ],
    },
    {
      title: '转发配置',
      fields: [
        {
          key: 'forwarders',
          label: '转发服务器',
          type: 'text',
          default: '8.8.8.8; 8.8.4.4',
          tip: '上游 DNS 服务器地址',
        },
        {
          key: 'forwardType',
          label: '转发策略',
          type: 'select',
          options: [
            { label: 'first - 先转发再自行解析', value: 'first' },
            { label: 'only - 仅转发不自行解析', value: 'only' },
          ],
          default: 'first',
        },
      ],
    },
    {
      title: '日志配置',
      fields: [
        {
          key: 'enableLogging',
          label: '启用日志',
          type: 'switch',
          default: true,
        },
        {
          key: 'logCategory',
          label: '日志类别',
          type: 'text',
          default: 'queries',
          dependsOn: { key: 'enableLogging', value: true },
          tip: 'BIND 日志类别名称',
        },
        {
          key: 'logFile',
          label: '日志文件路径',
          type: 'text',
          default: '/var/log/named/queries.log',
          dependsOn: { key: 'enableLogging', value: true },
        },
      ],
    },
    {
      title: 'DNSSEC',
      fields: [
        {
          key: 'enableDnssec',
          label: '启用 DNSSEC',
          type: 'switch',
          default: true,
          tip: '启用 DNS 安全扩展',
        },
        {
          key: 'dnssecValidation',
          label: 'DNSSEC 验证',
          type: 'switch',
          default: true,
          tip: '对查询结果进行 DNSSEC 验证',
        },
      ],
    },
    {
      title: '区域配置',
      fields: [
        {
          key: 'enableZone',
          label: '启用自定义区域',
          type: 'switch',
          default: false,
          tip: '配置本地 DNS 区域',
        },
        {
          key: 'zoneName',
          label: '区域名称',
          type: 'text',
          default: 'example.com',
          dependsOn: { key: 'enableZone', value: true },
        },
        {
          key: 'zoneType',
          label: '区域类型',
          type: 'select',
          options: [
            { label: 'master - 主区域', value: 'master' },
            { label: 'slave - 从区域', value: 'slave' },
            { label: 'hint - 根提示', value: 'hint' },
          ],
          default: 'master',
          dependsOn: { key: 'enableZone', value: true },
        },
        {
          key: 'zoneFile',
          label: '区域文件路径',
          type: 'text',
          default: '/var/named/example.com.zone',
          dependsOn: { key: 'enableZone', value: true },
        },
      ],
    },
  ],
}

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

  // Options block
  lines.push(`// ======================== 全局选项 ========================`)
  lines.push(`options {`)
  lines.push(`  listen-on port 53 { ${config.listenOn} };`)
  lines.push(`  listen-on-v6 port 53 { ${config.listenOnV6} };`)
  lines.push(`  directory       "/var/named";`)
  lines.push(`  dump-file       "/var/named/data/cache_dump.db";`)
  lines.push(`  statistics-file "/var/named/data/named_stats.txt";`)
  lines.push(`  memstatistics-file "/var/named/data/named_mem_stats.txt";`)
  lines.push(`  allow-query     { ${config.allowQuery} };`)
  lines.push(`  allow-recursion { ${config.allowRecursion} };`)
  lines.push(`  recursion ${config.recursion ? 'yes' : 'no'};`)
  lines.push(``)
  lines.push(`  forwarders {`)
  const forwarders = config.forwarders.split(';').map((s) => s.trim()).filter(Boolean)
  forwarders.forEach((f) => {
    lines.push(`    ${f};`)
  })
  lines.push(`  };`)
  lines.push(`  forward ${config.forwardType};`)
  lines.push(``)

  if (config.enableDnssec) {
    lines.push(`  dnssec-enable yes;`)
  }
  if (config.dnssecValidation) {
    lines.push(`  dnssec-validation yes;`)
  }
  lines.push(``)
  lines.push(`  pid-file "/run/named/named.pid";`)
  lines.push(`  session-keyfile "/run/named/session.key";`)
  lines.push(`};`)
  lines.push(``)

  // Logging
  if (config.enableLogging) {
    lines.push(`// ======================== 日志配置 ========================`)
    lines.push(`logging {`)
    lines.push(`  channel default_debug {`)
    lines.push(`    file "data/named.run";`)
    lines.push(`    severity dynamic;`)
    lines.push(`  };`)
    lines.push(`  channel query_log {`)
    lines.push(`    file "${config.logFile}" versions 3 size 5m;`)
    lines.push(`    severity dynamic;`)
    lines.push(`    print-time yes;`)
    lines.push(`    print-category yes;`)
    lines.push(`  };`)
    lines.push(`  category ${config.logCategory} { query_log; };`)
    lines.push(`};`)
    lines.push(``)
  }

  // Zone configuration
  lines.push(`// ======================== 标准区域 ========================`)
  lines.push(`zone "." IN {`)
  lines.push(`  type hint;`)
  lines.push(`  file "named.ca";`)
  lines.push(`};`)
  lines.push(``)
  lines.push(`zone "localhost" IN {`)
  lines.push(`  type master;`)
  lines.push(`  file "named.localhost";`)
  lines.push(`  allow-update { none; };`)
  lines.push(`};`)
  lines.push(``)
  lines.push(`zone "0.0.127.in-addr.arpa" IN {`)
  lines.push(`  type master;`)
  lines.push(`  file "named.loopback";`)
  lines.push(`  allow-update { none; };`)
  lines.push(`};`)
  lines.push(``)

  if (config.enableZone) {
    lines.push(`// ======================== 自定义区域 ========================`)
    lines.push(`zone "${config.zoneName}" IN {`)
    lines.push(`  type ${config.zoneType};`)
    lines.push(`  file "${config.zoneFile}";`)
    if (config.zoneType === 'master') {
      lines.push(`  allow-update { none; };`)
    }
    lines.push(`};`)
    lines.push(``)
  }

  lines.push(`include "/etc/named.rfc1912.zones";`)
  lines.push(`include "/etc/named.root.key";`)

  return lines.join('\n')
}
