export const dhcpSchema = {
  id: 'dhcp',
  name: 'DHCP',
  icon: 'Connection',
  category: '系统服务',
  description: 'ISC DHCP 服务器配置',
  format: 'conf',
  fileName: 'dhcpd.conf',
  groups: [
    {
      title: '全局参数',
      fields: [
        {
          key: 'domainName',
          label: '域名',
          type: 'text',
          default: 'example.com',
          tip: '分配给客户端的域名',
        },
        {
          key: 'domainNameServers',
          label: 'DNS 服务器',
          type: 'text',
          default: '8.8.8.8, 8.8.4.4',
          tip: '多个用逗号分隔',
        },
        {
          key: 'defaultLeaseTime',
          label: '默认租约时间 (秒)',
          type: 'number',
          min: 60,
          max: 86400,
          default: 600,
        },
        {
          key: 'maxLeaseTime',
          label: '最大租约时间 (秒)',
          type: 'number',
          min: 60,
          max: 604800,
          default: 7200,
        },
      ],
    },
    {
      title: '子网配置',
      fields: [
        {
          key: 'subnet',
          label: '子网地址',
          type: 'text',
          default: '192.168.1.0',
          tip: 'DHCP 分配的子网',
        },
        {
          key: 'netmask',
          label: '子网掩码',
          type: 'text',
          default: '255.255.255.0',
        },
        {
          key: 'rangeStart',
          label: '地址池起始',
          type: 'text',
          default: '192.168.1.100',
          tip: '动态分配范围起始 IP',
        },
        {
          key: 'rangeEnd',
          label: '地址池结束',
          type: 'text',
          default: '192.168.1.200',
          tip: '动态分配范围结束 IP',
        },
        {
          key: 'gateway',
          label: '默认网关',
          type: 'text',
          default: '192.168.1.1',
        },
        {
          key: 'broadcastAddress',
          label: '广播地址',
          type: 'text',
          default: '192.168.1.255',
        },
      ],
    },
    {
      title: '静态主机绑定',
      fields: [
        {
          key: 'enableStaticHost',
          label: '启用静态主机',
          type: 'switch',
          default: false,
          tip: '为特定 MAC 地址分配固定 IP',
        },
        {
          key: 'staticHostName',
          label: '主机名',
          type: 'text',
          default: 'myhost',
          dependsOn: { key: 'enableStaticHost', value: true },
        },
        {
          key: 'staticHostMac',
          label: 'MAC 地址',
          type: 'text',
          default: '00:11:22:33:44:55',
          dependsOn: { key: 'enableStaticHost', value: true },
          tip: '客户端 MAC 地址',
        },
        {
          key: 'staticHostIp',
          label: '固定 IP 地址',
          type: 'text',
          default: '192.168.1.50',
          dependsOn: { key: 'enableStaticHost', value: true },
        },
      ],
    },
  ],
}

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

  // Global parameters
  lines.push(`# ======================== 全局参数 ========================`)
  lines.push(`option domain-name "${config.domainName}";`)
  const dnsServers = config.domainNameServers.split(',').map((s) => s.trim()).join(', ')
  lines.push(`option domain-name-servers ${dnsServers};`)
  lines.push(``)
  lines.push(`default-lease-time ${config.defaultLeaseTime};`)
  lines.push(`max-lease-time ${config.maxLeaseTime};`)
  lines.push(``)
  lines.push(`# 如果本机不是 DHCP 服务器的 authoritative，取消注释下行`)
  lines.push(`# authoritative;`)
  lines.push(``)

  // Subnet configuration
  lines.push(`# ======================== 子网配置 ========================`)
  lines.push(`subnet ${config.subnet} netmask ${config.netmask} {`)
  lines.push(`  range ${config.rangeStart} ${config.rangeEnd};`)
  lines.push(`  option routers ${config.gateway};`)
  lines.push(`  option broadcast-address ${config.broadcastAddress};`)
  lines.push(`  option domain-name-servers ${dnsServers};`)
  lines.push(`  option domain-name "${config.domainName}";`)
  lines.push(`  default-lease-time ${config.defaultLeaseTime};`)
  lines.push(`  max-lease-time ${config.maxLeaseTime};`)
  lines.push(`}`)
  lines.push(``)

  // Static host
  if (config.enableStaticHost) {
    lines.push(`# ======================== 静态主机绑定 ========================`)
    lines.push(`host ${config.staticHostName} {`)
    lines.push(`  hardware ethernet ${config.staticHostMac};`)
    lines.push(`  fixed-address ${config.staticHostIp};`)
    lines.push(`}`)
    lines.push(``)
  }

  return lines.join('\n')
}
