// K8s PersistentVolume schema + YAML generator for ConfTemplate
// Generated: 2026-07-18

export const k8sPvSchema = {
  id: 'k8s-pv',
  name: 'K8s PersistentVolume',
  icon: 'Database',
  category: '云原生',
  description: 'Kubernetes 持久化存储卷 (PV)',
  format: 'yaml',
  fileName: 'pv.yaml',
  groups: [
    {
      title: '基础信息',
      fields: [
        {
          key: 'name',
          label: 'PV 名称',
          type: 'text',
          placeholder: 'my-pv',
          default: 'my-pv',
          required: true,
          tip: 'PersistentVolume 资源名称',
        },
        {
          key: 'capacity',
          label: '存储容量',
          type: 'text',
          placeholder: '10Gi',
          default: '10Gi',
          required: true,
          tip: '例如 10Gi, 100Mi',
        },
        {
          key: 'accessModes',
          label: '访问模式',
          type: 'select',
          options: [
            { label: 'ReadWriteOnce', value: 'ReadWriteOnce' },
            { label: 'ReadWriteMany', value: 'ReadWriteMany' },
            { label: 'ReadOnlyMany', value: 'ReadOnlyMany' },
          ],
          default: 'ReadWriteOnce',
          tip: '单节点读写 / 多节点读写 / 多节点只读',
        },
        {
          key: 'storageClassName',
          label: 'StorageClass 名称',
          type: 'text',
          placeholder: 'standard',
          default: 'standard',
        },
        {
          key: 'persistentVolumeReclaimPolicy',
          label: '回收策略',
          type: 'select',
          options: [
            { label: 'Retain', value: 'Retain' },
            { label: 'Recycle', value: 'Recycle' },
            { label: 'Delete', value: 'Delete' },
          ],
          default: 'Retain',
          tip: 'PV 释放后的处理方式',
        },
        {
          key: 'volumeMode',
          label: '卷模式',
          type: 'select',
          options: [
            { label: 'Filesystem', value: 'Filesystem' },
            { label: 'Block', value: 'Block' },
          ],
          default: 'Filesystem',
        },
      ],
    },
    {
      title: 'HostPath 配置',
      fields: [
        {
          key: 'hostPathEnabled',
          label: '启用 HostPath',
          type: 'switch',
          default: false,
          tip: '使用节点本地路径作为存储',
        },
        {
          key: 'hostPath',
          label: 'HostPath 路径',
          type: 'text',
          placeholder: '/tmp/pv',
          default: '/tmp/pv',
          dependsOn: 'hostPathEnabled',
        },
      ],
    },
    {
      title: 'NFS 配置',
      fields: [
        {
          key: 'nfsEnabled',
          label: '启用 NFS',
          type: 'switch',
          default: false,
          tip: '使用 NFS 共享存储',
        },
        {
          key: 'nfsServer',
          label: 'NFS 服务器地址',
          type: 'text',
          placeholder: '192.168.1.100',
          default: '',
          dependsOn: 'nfsEnabled',
        },
        {
          key: 'nfsPath',
          label: 'NFS 路径',
          type: 'text',
          placeholder: '/exports/data',
          default: '',
          dependsOn: 'nfsEnabled',
        },
      ],
    },
  ],
}

function toYaml(obj, indent = 0) {
  const lines = []
  const prefix = '  '.repeat(indent)

  for (const [key, value] of Object.entries(obj)) {
    if (value === null || value === undefined || value === '') continue

    if (Array.isArray(value)) {
      lines.push(`${prefix}${key}:`)
      for (const item of value) {
        if (typeof item === 'object' && item !== null) {
          const entries = Object.entries(item).filter(([, v]) => v !== null && v !== undefined && v !== '')
          if (entries.length === 0) continue
          const first = entries[0]
          lines.push(`${prefix}  - ${first[0]}: ${first[1]}`)
          for (let i = 1; i < entries.length; i++) {
            lines.push(`${prefix}    ${entries[i][0]}: ${entries[i][1]}`)
          }
        } else {
          lines.push(`${prefix}  - ${item}`)
        }
      }
    } else if (typeof value === 'object') {
      lines.push(`${prefix}${key}:`)
      lines.push(toYaml(value, indent + 1))
    } else {
      lines.push(`${prefix}${key}: ${value}`)
    }
  }

  return lines.join('\n')
}

export function generateK8sPvYaml(config) {
  const spec = {
    capacity: {
      storage: config.capacity,
    },
    accessModes: [config.accessModes],
    persistentVolumeReclaimPolicy: config.persistentVolumeReclaimPolicy,
    storageClassName: config.storageClassName,
    volumeMode: config.volumeMode,
  }

  if (config.hostPathEnabled) {
    spec.hostPath = {
      path: config.hostPath,
    }
  } else if (config.nfsEnabled) {
    spec.nfs = {
      server: config.nfsServer,
      path: config.nfsPath,
    }
  }

  const pv = {
    apiVersion: 'v1',
    kind: 'PersistentVolume',
    metadata: {
      name: config.name,
    },
    spec,
  }

  const header = `# K8s PersistentVolume - 由 ConfTemplate 生成
# 生成时间: ${new Date().toLocaleString('zh-CN')}
# 部署命令: kubectl apply -f ${config.fileName || 'pv.yaml'}
`

  return header + '\n' + toYaml(pv)
}
