export const dockerSchema = {
  id: 'docker',
  name: 'Docker',
  icon: 'Box',
  category: '容器化',
  description: 'Docker 守护进程配置',
  format: 'json',
  fileName: 'daemon.json',
  groups: [
    {
      title: '存储配置',
      fields: [
        {
          key: 'dataRoot',
          label: '数据根目录',
          type: 'text',
          default: '/var/lib/docker',
          tip: 'Docker 数据存储路径',
        },
        {
          key: 'storageDriver',
          label: '存储驱动',
          type: 'select',
          options: [
            { label: 'overlay2 (推荐)', value: 'overlay2' },
            { label: 'aufs', value: 'aufs' },
            { label: 'btrfs', value: 'btrfs' },
          ],
          default: 'overlay2',
        },
      ],
    },
    {
      title: '日志配置',
      fields: [
        {
          key: 'logDriver',
          label: '日志驱动',
          type: 'select',
          options: [
            { label: 'json-file (默认)', value: 'json-file' },
            { label: 'syslog', value: 'syslog' },
            { label: 'journald', value: 'journald' },
            { label: 'fluentd', value: 'fluentd' },
          ],
          default: 'json-file',
        },
        {
          key: 'logMaxSize',
          label: '单个日志文件最大大小',
          type: 'text',
          default: '10m',
          tip: '例如 10m、100m、1g',
        },
        {
          key: 'logMaxFile',
          label: '最大日志文件数',
          type: 'number',
          min: 1,
          max: 10,
          default: 3,
        },
      ],
    },
    {
      title: '镜像仓库',
      fields: [
        {
          key: 'registryMirrors',
          label: '镜像加速地址',
          type: 'text',
          default: 'https://mirror.ccs.tencentyun.com',
          tip: '多个用逗号分隔',
        },
        {
          key: 'insecureRegistries',
          label: '非安全仓库',
          type: 'text',
          default: '',
          placeholder: '例如: 192.168.1.100:5000',
          tip: '留空则不设置',
        },
      ],
    },
    {
      title: '运行时配置',
      fields: [
        {
          key: 'liveRestore',
          label: '实时恢复',
          type: 'switch',
          default: true,
          tip: 'Docker 守护进程重启时保持容器运行',
        },
        {
          key: 'experimental',
          label: '实验性功能',
          type: 'switch',
          default: false,
          tip: '开启实验性特性',
        },
        {
          key: 'defaultRuntime',
          label: '默认运行时',
          type: 'select',
          options: [
            { label: 'runc (标准)', value: 'runc' },
            { label: 'nvidia (GPU)', value: 'nvidia' },
          ],
          default: 'runc',
          tip: 'GPU 场景选择 nvidia',
        },
        {
          key: 'maxConcurrentDownloads',
          label: '最大并发下载数',
          type: 'number',
          min: 1,
          max: 20,
          default: 3,
        },
        {
          key: 'maxConcurrentUploads',
          label: '最大并发上传数',
          type: 'number',
          min: 1,
          max: 10,
          default: 5,
        },
      ],
    },
    {
      title: '网络配置',
      fields: [
        {
          key: 'bip',
          label: 'docker0 网桥 IP',
          type: 'text',
          default: '172.17.0.1/16',
          tip: 'docker0 网桥 IP',
        },
        {
          key: 'defaultAddressPool',
          label: '自定义网络地址池',
          type: 'text',
          default: '10.10.0.0/16',
          placeholder: '例如: 10.10.0.0/16',
          tip: '自定义网络地址池',
        },
        {
          key: 'dns',
          label: 'DNS 服务器',
          type: 'text',
          default: '8.8.8.8,8.8.4.4',
          tip: '多个用逗号分隔',
        },
      ],
    },
    {
      title: '监控与指标',
      fields: [
        {
          key: 'enableMetrics',
          label: '开启指标收集',
          type: 'switch',
          default: false,
          tip: '暴露 Prometheus 格式指标',
        },
        {
          key: 'metricsAddress',
          label: '指标监听地址',
          type: 'text',
          default: '0.0.0.0:9323',
          dependsOn: { key: 'enableMetrics', value: true },
        },
      ],
    },
    {
      title: 'TLS 安全',
      fields: [
        {
          key: 'enableTls',
          label: '启用 TLS',
          type: 'switch',
          default: false,
          tip: '启用 Docker API 的 TLS 加密通信',
        },
        {
          key: 'tlscacert',
          label: 'CA 证书路径',
          type: 'text',
          default: '/etc/docker/ca.pem',
          dependsOn: { key: 'enableTls', value: true },
        },
        {
          key: 'tlscert',
          label: '服务端证书路径',
          type: 'text',
          default: '/etc/docker/server.pem',
          dependsOn: { key: 'enableTls', value: true },
        },
        {
          key: 'tlskey',
          label: '服务端私钥路径',
          type: 'text',
          default: '/etc/docker/server-key.pem',
          dependsOn: { key: 'enableTls', value: true },
        },
      ],
    },
  ],
}

export function generateDockerDaemonJson(config) {
  const obj = {}

  // 存储配置
  obj['data-root'] = config.dataRoot
  obj['storage-driver'] = config.storageDriver

  // 日志配置
  obj['log-driver'] = config.logDriver
  obj['log-opts'] = {
    'max-size': config.logMaxSize,
    'max-file': String(config.logMaxFile),
  }

  // 镜像仓库
  if (config.registryMirrors) {
    obj['registry-mirrors'] = config.registryMirrors.split(',').map((m) => m.trim())
  }
  if (config.insecureRegistries) {
    obj['insecure-registries'] = config.insecureRegistries.split(',').map((r) => r.trim())
  }

  // 运行时配置
  obj['live-restore'] = config.liveRestore
  obj['experimental'] = config.experimental
  if (config.defaultRuntime !== 'runc') {
    obj['default-runtime'] = config.defaultRuntime
  }
  obj['max-concurrent-downloads'] = config.maxConcurrentDownloads
  obj['max-concurrent-uploads'] = config.maxConcurrentUploads

  // 网络配置
  obj['bip'] = config.bip
  if (config.defaultAddressPool) {
    obj['default-address-pools'] = [
      { base: config.defaultAddressPool, size: 24 },
    ]
  }
  if (config.dns) {
    obj['dns'] = config.dns.split(',').map((d) => d.trim())
  }

  // 监控与指标
  if (config.enableMetrics) {
    obj['metrics-addr'] = config.metricsAddress
    obj['experimental'] = true
  }

  // TLS 安全
  if (config.enableTls) {
    obj['tlsverify'] = true
    obj['tlscacert'] = config.tlscacert
    obj['tlscert'] = config.tlscert
    obj['tlskey'] = config.tlskey
  }

  return JSON.stringify(obj, null, 2)
}
