export const keepalivedSchema = {
  id: 'keepalived',
  name: 'Keepalived',
  icon: 'Connection',
  category: '高可用',
  description: '基于 VRRP 协议的高可用解决方案，用于 IP 漂移和故障转移',
  format: 'conf',
  fileName: 'keepalived.conf',
  groups: [
    {
      title: 'VRRP 实例配置',
      fields: [
        {
          key: 'vrrpInstanceName',
          label: 'VRRP 实例名称',
          type: 'text',
          default: 'VI_1',
        },
        {
          key: 'interface',
          label: '网络接口',
          type: 'text',
          default: 'eth0',
          tip: '绑定的网络接口名称',
        },
        {
          key: 'state',
          label: '初始状态',
          type: 'select',
          options: [
            { label: 'MASTER - 主节点', value: 'master' },
            { label: 'BACKUP - 备节点', value: 'backup' },
          ],
          default: 'master',
        },
        {
          key: 'virtualRouterId',
          label: '虚拟路由 ID',
          type: 'number',
          min: 1,
          max: 255,
          default: 51,
          tip: '同一组的节点必须相同',
        },
        {
          key: 'priority',
          label: '优先级',
          type: 'number',
          min: 1,
          max: 255,
          default: 100,
          tip: '数值越大优先级越高',
        },
        {
          key: 'advertInt',
          label: '通告间隔 (秒)',
          type: 'number',
          min: 1,
          max: 255,
          default: 1,
        },
      ],
    },
    {
      title: '认证配置',
      fields: [
        {
          key: 'authType',
          label: '认证类型',
          type: 'text',
          default: 'PASS',
          tip: 'PASS 或 AH',
        },
        {
          key: 'authPass',
          label: '认证密码',
          type: 'text',
          default: '1234',
          tip: '同一组的节点必须相同',
        },
      ],
    },
    {
      title: '虚拟 IP 配置',
      fields: [
        {
          key: 'virtualIpAddress',
          label: '虚拟 IP 地址',
          type: 'text',
          default: '192.168.1.100/24 dev eth0',
          required: true,
          tip: '格式: IP/MASK dev 接口',
        },
      ],
    },
    {
      title: '通知脚本',
      fields: [
        {
          key: 'enableNotify',
          label: '启用通知脚本',
          type: 'switch',
          default: false,
          tip: '状态切换时执行的脚本',
        },
        {
          key: 'notifyMaster',
          label: '切换为 Master 时执行',
          type: 'text',
          default: "'/etc/keepalived/notify.sh master'",
          dependsOn: { key: 'enableNotify', value: true },
        },
        {
          key: 'notifyBackup',
          label: '切换为 Backup 时执行',
          type: 'text',
          default: "'/etc/keepalived/notify.sh backup'",
          dependsOn: { key: 'enableNotify', value: true },
        },
        {
          key: 'notifyFault',
          label: '进入 Fault 状态时执行',
          type: 'text',
          default: "'/etc/keepalived/notify.sh fault'",
          dependsOn: { key: 'enableNotify', value: true },
        },
      ],
    },
    {
      title: '单播配置',
      fields: [
        {
          key: 'enableVipUnicast',
          label: '启用单播',
          type: 'switch',
          default: false,
          tip: '使用单播代替组播通信',
        },
        {
          key: 'unicastPeerIp',
          label: '单播对端 IP',
          type: 'text',
          default: '192.168.1.2',
          dependsOn: { key: 'enableVipUnicast', value: true },
        },
      ],
    },
    {
      title: '健康检查',
      fields: [
        {
          key: 'enableHealthCheck',
          label: '启用健康检查',
          type: 'switch',
          default: true,
        },
        {
          key: 'healthCheckType',
          label: '检查类型',
          type: 'select',
          options: [
            { label: 'TCP 端口检查', value: 'tcp' },
            { label: 'HTTP GET 检查', value: 'http_get' },
            { label: 'SSL GET 检查', value: 'ssl_get' },
            { label: 'SMTP 检查', value: 'smtp' },
            { label: 'MISC 自定义检查', value: 'misc_check' },
          ],
          default: 'tcp',
          dependsOn: { key: 'enableHealthCheck', value: true },
        },
        {
          key: 'healthCheckUrl',
          label: '检查 URL 路径',
          type: 'text',
          default: '/health',
          dependsOn: { key: 'healthCheckType', valueIn: ['http_get', 'ssl_get'] },
        },
        {
          key: 'healthCheckPort',
          label: '检查端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 80,
          dependsOn: { key: 'enableHealthCheck', value: true },
        },
        {
          key: 'healthCheckTimeout',
          label: '超时时间 (秒)',
          type: 'number',
          min: 1,
          max: 30,
          default: 3,
          dependsOn: { key: 'enableHealthCheck', value: true },
        },
        {
          key: 'healthCheckRetry',
          label: '重试次数',
          type: 'number',
          min: 1,
          max: 10,
          default: 3,
          dependsOn: { key: 'enableHealthCheck', value: true },
        },
        {
          key: 'healthCheckDelayBeforeRetry',
          label: '重试间隔 (秒)',
          type: 'number',
          min: 1,
          max: 30,
          default: 3,
          dependsOn: { key: 'enableHealthCheck', value: true },
        },
      ],
    },
  ],
}

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

  // VRRP Script (health check)
  if (config.enableHealthCheck) {
    lines.push(`vrrp_script chk_service {`)
    if (config.healthCheckType === 'tcp') {
      lines.push(`    script "</dev/tcp/127.0.0.1/${config.healthCheckPort}"`)
    } else if (config.healthCheckType === 'http_get') {
      lines.push(`    script "curl -s http://127.0.0.1:${config.healthCheckPort}${config.healthCheckUrl}"`)
    } else if (config.healthCheckType === 'ssl_get') {
      lines.push(`    script "curl -sk https://127.0.0.1:${config.healthCheckPort}${config.healthCheckUrl}"`)
    } else if (config.healthCheckType === 'smtp') {
      lines.push(`    script "nc -w ${config.healthCheckTimeout} 127.0.0.1 25 < /dev/null"`)
    } else {
      lines.push(`    script "/etc/keepalived/check_script.sh"`)
    }
    lines.push(`    interval ${config.healthCheckTimeout}`)
    lines.push(`    weight -20`)
    lines.push(`    fall ${config.healthCheckRetry}`)
    lines.push(`    rise ${config.healthCheckRetry}`)
    lines.push(`}`)
    lines.push(``)
  }

  // VRRP Instance
  lines.push(`vrrp_instance ${config.vrrpInstanceName} {`)
  lines.push(`    state ${config.state.toUpperCase()}`)
  lines.push(`    interface ${config.interface}`)
  lines.push(`    virtual_router_id ${config.virtualRouterId}`)
  lines.push(`    priority ${config.priority}`)
  lines.push(`    advert_int ${config.advertInt}`)
  lines.push(``)

  // Auth
  lines.push(`    authentication {`)
  lines.push(`        auth_type ${config.authType}`)
  lines.push(`        auth_pass ${config.authPass}`)
  lines.push(`    }`)
  lines.push(``)

  // Virtual IP
  lines.push(`    virtual_ipaddress {`)
  lines.push(`        ${config.virtualIpAddress}`)
  lines.push(`    }`)
  lines.push(``)

  // Unicast
  if (config.enableVipUnicast) {
    lines.push(`    unicast_src_ip ${config.virtualIpAddress.split('/')[0].trim()}`)
    lines.push(`    unicast_peer {`)
    lines.push(`        ${config.unicastPeerIp}`)
    lines.push(`    }`)
    lines.push(``)
  }

  // Notify
  if (config.enableNotify) {
    lines.push(`    notify_master ${config.notifyMaster}`)
    lines.push(`    notify_backup ${config.notifyBackup}`)
    lines.push(`    notify_fault ${config.notifyFault}`)
    lines.push(``)
  }

  // Health check track
  if (config.enableHealthCheck) {
    lines.push(`    track_script {`)
    lines.push(`        chk_service`)
    lines.push(`    }`)
  }

  lines.push(`}`)

  return lines.join('\n')
}
