export const nfsSchema = {
  id: 'nfs',
  name: 'NFS',
  icon: 'Files',
  category: '系统服务',
  description: 'NFS 网络文件系统 exports 配置',
  format: 'conf',
  fileName: 'exports',
  groups: [
    {
      title: '主共享配置',
      fields: [
        {
          key: 'sharePath',
          label: '共享路径',
          type: 'text',
          default: '/data/share',
          tip: '要共享的本地目录路径',
        },
        {
          key: 'clientAddress',
          label: '客户端地址',
          type: 'text',
          default: '192.168.1.0/24',
          tip: '允许访问的客户端网段或主机',
        },
        {
          key: 'permissions',
          label: '权限',
          type: 'select',
          options: [
            { label: 'rw - 读写', value: 'rw' },
            { label: 'ro - 只读', value: 'ro' },
          ],
          default: 'rw',
        },
        {
          key: 'rootSquash',
          label: 'Root Squash',
          type: 'switch',
          default: true,
          tip: '将 root 用户映射为匿名用户',
        },
        {
          key: 'allSquash',
          label: 'All Squash',
          type: 'switch',
          default: false,
          tip: '将所有用户映射为匿名用户',
        },
        {
          key: 'sync',
          label: '同步写入',
          type: 'switch',
          default: true,
          tip: '同步写入磁盘，更安全但稍慢',
        },
        {
          key: 'anonuid',
          label: '匿名用户 UID',
          type: 'number',
          min: 0,
          max: 65535,
          default: 65534,
          tip: '匿名用户的 UID',
        },
        {
          key: 'anongid',
          label: '匿名用户 GID',
          type: 'number',
          min: 0,
          max: 65535,
          default: 65534,
          tip: '匿名用户的 GID',
        },
      ],
    },
    {
      title: '附加共享',
      fields: [
        {
          key: 'additionalShares',
          label: '启用附加共享',
          type: 'switch',
          default: false,
          tip: '配置第二个 NFS 共享目录',
        },
        {
          key: 'sharePath2',
          label: '共享路径 2',
          type: 'text',
          default: '/data/backup',
          dependsOn: { key: 'additionalShares', value: true },
        },
        {
          key: 'clientAddress2',
          label: '客户端地址 2',
          type: 'text',
          default: '10.0.0.0/24',
          dependsOn: { key: 'additionalShares', value: true },
        },
        {
          key: 'permissions2',
          label: '权限 2',
          type: 'select',
          options: [
            { label: 'rw - 读写', value: 'rw' },
            { label: 'ro - 只读', value: 'ro' },
          ],
          default: 'ro',
          dependsOn: { key: 'additionalShares', value: true },
        },
      ],
    },
    {
      title: 'NFS 版本',
      fields: [
        {
          key: 'enableNfsV4',
          label: '启用 NFSv4',
          type: 'switch',
          default: true,
          tip: '使用 NFS 第四版协议',
        },
      ],
    },
  ],
}

export function generateNfsExports(config) {
  const lines = []
  lines.push(`# NFS exports 配置文件 - 由 ConfTemplate 生成`)
  lines.push(`# 生成时间: ${new Date().toLocaleString('zh-CN')}`)
  lines.push(`# 格式: <目录> <客户端>(<选项>)`)
  lines.push(``)

  // Build options string for primary share
  const buildOptions = (cfg, prefix = '') => {
    const perms = cfg[`permissions${prefix}`] || 'rw'
    const rootSquash = cfg.rootSquash !== undefined ? cfg.rootSquash : true
    const allSquash = cfg.allSquash !== undefined ? cfg.allSquash : false
    const sync = cfg.sync !== undefined ? cfg.sync : true

    const opts = []
    opts.push(perms)
    opts.push(sync ? 'sync' : 'async')
    opts.push(rootSquash ? 'root_squash' : 'no_root_squash')
    if (allSquash) {
      opts.push('all_squash')
    }
    opts.push(`anonuid=${cfg.anonuid || 65534}`)
    opts.push(`anongid=${cfg.anongid || 65534}`)

    return opts.join(',')
  }

  // Primary share
  lines.push(`# ======================== 主共享 ========================`)
  lines.push(`${config.sharePath}    ${config.clientAddress}(${buildOptions(config)})`)
  lines.push(``)

  // Additional share
  if (config.additionalShares) {
    lines.push(`# ======================== 附加共享 ========================`)
    const opts2 = []
    opts2.push(config.permissions2 || 'ro')
    opts2.push('sync')
    opts2.push('root_squash')
    opts2.push(`anonuid=${config.anonuid || 65534}`)
    opts2.push(`anongid=${config.anongid || 65534}`)
    lines.push(`${config.sharePath2}    ${config.clientAddress2}(${opts2.join(',')})`)
    lines.push(``)
  }

  // NFSv4 note
  if (config.enableNfsV4) {
    lines.push(`# ======================== NFSv4 配置提示 ========================`)
    lines.push(`# NFSv4 需要在 /etc/exports 中使用 fsid=0 或 fsid=N 来定义导出根`)
    lines.push(`# 确保 rpcbind 和 nfs-server 服务已启动:`)
    lines.push(`#   systemctl enable --now rpcbind nfs-server`)
    lines.push(`# 修改后执行: exportfs -ra`)
  }

  return lines.join('\n')
}
