export const ansibleSchema = {
  id: 'ansible',
  name: 'Ansible',
  icon: 'Files',
  category: '自动化运维',
  description: '自动化配置管理与应用部署工具',
  format: 'cfg',
  fileName: 'ansible.cfg',
  groups: [
    {
      title: '基础配置',
      fields: [
        {
          key: 'inventory',
          label: 'Inventory 文件路径',
          type: 'text',
          default: '/etc/ansible/hosts',
          tip: '主机清单文件路径',
        },
        {
          key: 'remoteUser',
          label: '远程用户',
          type: 'text',
          placeholder: 'root',
          default: 'root',
        },
        {
          key: 'hostKeyChecking',
          label: 'SSH 主机密钥检查',
          type: 'switch',
          default: false,
          tip: '首次连接是否检查主机密钥，测试环境建议关闭',
        },
        {
          key: 'retryFilesEnabled',
          label: '启用重试文件',
          type: 'switch',
          default: false,
          tip: '失败时生成 .retry 文件',
        },
      ],
    },
    {
      title: 'SSH 连接',
      fields: [
        {
          key: 'privateKeyFile',
          label: 'SSH 私钥路径',
          type: 'text',
          placeholder: '~/.ssh/id_rsa',
          default: '~/.ssh/id_rsa',
        },
        {
          key: 'sshPort',
          label: 'SSH 默认端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 22,
        },
        {
          key: 'timeout',
          label: '连接超时 (秒)',
          type: 'number',
          min: 5,
          max: 300,
          default: 30,
        },
        {
          key: 'pipelining',
          label: '开启 Pipelining',
          type: 'switch',
          default: true,
          tip: '减少 SSH 连接次数，提升执行效率',
        },
        {
          key: 'sshArgs',
          label: 'SSH 额外参数',
          type: 'text',
          placeholder: '-o ControlMaster=auto -o ControlPersist=60s',
          default: '-o ControlMaster=auto -o ControlPersist=60s',
          tip: '控制 SSH 连接复用',
        },
      ],
    },
    {
      title: '权限提升 (Become)',
      fields: [
        {
          key: 'become',
          label: '默认开启提权',
          type: 'switch',
          default: false,
          tip: '是否默认使用 sudo 提权',
        },
        {
          key: 'becomeMethod',
          label: '提权方式',
          type: 'select',
          options: [
            { label: 'sudo', value: 'sudo' },
            { label: 'su', value: 'su' },
            { label: 'pbrun', value: 'pbrun' },
            { label: 'pfexec', value: 'pfexec' },
          ],
          default: 'sudo',
          dependsOn: { key: 'become', value: true },
        },
        {
          key: 'becomeUser',
          label: '提权目标用户',
          type: 'text',
          default: 'root',
          dependsOn: { key: 'become', value: true },
        },
        {
          key: 'becomeAskPass',
          label: '询问提权密码',
          type: 'switch',
          default: false,
          dependsOn: { key: 'become', value: true },
        },
      ],
    },
    {
      title: '执行控制',
      fields: [
        {
          key: 'forks',
          label: '并发进程数 (forks)',
          type: 'number',
          min: 1,
          max: 200,
          default: 20,
          tip: '同时操作的主机数量',
        },
        {
          key: 'pollInterval',
          label: '轮询间隔 (秒)',
          type: 'number',
          min: 1,
          max: 60,
          default: 15,
        },
        {
          key: 'timeoutTask',
          label: '任务超时 (秒)',
          type: 'number',
          min: 10,
          max: 3600,
          default: 300,
        },
      ],
    },
    {
      title: '日志与输出',
      fields: [
        {
          key: 'logPath',
          label: '日志文件路径',
          type: 'text',
          placeholder: '/var/log/ansible.log',
          default: '',
          tip: '留空表示不记录到文件',
        },
        {
          key: 'stdoutCallback',
          label: '输出回调插件',
          type: 'select',
          options: [
            { label: 'default - 默认格式', value: 'default' },
            { label: 'yaml - YAML 格式 (推荐)', value: 'yaml' },
            { label: 'json - JSON 格式', value: 'json' },
            { label: 'minimal - 最小化输出', value: 'minimal' },
            { label: 'dense - 密集输出', value: 'dense' },
          ],
          default: 'yaml',
        },
        {
          key: 'displaySkippedHosts',
          label: '显示跳过的主机',
          type: 'switch',
          default: false,
        },
        {
          key: 'displayOkHosts',
          label: '显示成功的主机',
          type: 'switch',
          default: true,
        },
      ],
    },
    {
      title: '事实收集',
      fields: [
        {
          key: 'gathering',
          label: '事实收集策略',
          type: 'select',
          options: [
            { label: 'smart - 智能缓存 (推荐)', value: 'smart' },
            { label: 'implicit - 每次收集', value: 'implicit' },
            { label: 'explicit - 仅显式收集', value: 'explicit' },
          ],
          default: 'smart',
        },
        {
          key: 'factCaching',
          label: '事实缓存方式',
          type: 'select',
          options: [
            { label: 'memory - 不缓存 (默认)', value: 'memory' },
            { label: 'jsonfile - JSON 文件缓存', value: 'jsonfile' },
            { label: 'redis - Redis 缓存', value: 'redis' },
          ],
          default: 'memory',
        },
        {
          key: 'factCachingConnection',
          label: '缓存路径/连接',
          type: 'text',
          placeholder: '/tmp/ansible_facts_cache',
          default: '/tmp/ansible_facts_cache',
          dependsOn: { key: 'factCaching', value: 'jsonfile' },
        },
      ],
    },
  ],
}

export function generateAnsibleCfg(config) {
  const lines = []
  lines.push(`# Ansible 配置文件 - 由 ConfTemplate 生成`)
  lines.push(`# 生成时间: ${new Date().toLocaleString('zh-CN')}`)
  lines.push(``)
  lines.push(`[defaults]`)
  lines.push(`inventory = ${config.inventory}`)
  lines.push(`remote_user = ${config.remoteUser}`)
  lines.push(`host_key_checking = ${config.hostKeyChecking ? 'True' : 'False'}`)
  lines.push(`retry_files_enabled = ${config.retryFilesEnabled ? 'True' : 'False'}`)
  lines.push(`forks = ${config.forks}`)
  lines.push(`poll_interval = ${config.pollInterval}`)
  lines.push(`timeout = ${config.timeoutTask}`)
  lines.push(`stdout_callback = ${config.stdoutCallback}`)
  lines.push(`display_skipped_hosts = ${config.displaySkippedHosts ? 'True' : 'False'}`)
  lines.push(`display_ok_hosts = ${config.displayOkHosts ? 'True' : 'False'}`)
  lines.push(`gathering = ${config.gathering}`)

  if (config.factCaching !== 'memory') {
    lines.push(`fact_caching = ${config.factCaching}`)
    lines.push(`fact_caching_connection = ${config.factCachingConnection}`)
    lines.push(`fact_caching_timeout = 86400`)
  }

  if (config.logPath) {
    lines.push(`log_path = ${config.logPath}`)
  }

  lines.push(``)
  lines.push(`[ssh_connection]`)
  lines.push(`ssh_args = ${config.sshArgs}`)
  lines.push(`pipelining = ${config.pipelining ? 'True' : 'False'}`)
  lines.push(`control_path = /tmp/ansible-%%h-%%r`)
  lines.push(`timeout = ${config.timeout}`)

  if (config.privateKeyFile) {
    lines.push(`private_key_file = ${config.privateKeyFile}`)
  }

  if (config.become) {
    lines.push(``)
    lines.push(`[privilege_escalation]`)
    lines.push(`become = True`)
    lines.push(`become_method = ${config.becomeMethod}`)
    lines.push(`become_user = ${config.becomeUser}`)
    lines.push(`become_ask_pass = ${config.becomeAskPass ? 'True' : 'False'}`)
  }

  lines.push(``)
  lines.push(`[inventory]`)
  lines.push(`enable_plugins = host_list, script, yaml, ini, auto`)

  return lines.join('\n')
}
