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

export const k8sStorageClassSchema = {
  id: 'k8s-storageclass',
  name: 'K8s StorageClass',
  icon: 'Layers',
  category: '云原生',
  description: 'Kubernetes 存储类 (StorageClass)',
  format: 'yaml',
  fileName: 'storageclass.yaml',
  groups: [
    {
      title: '基础信息',
      fields: [
        {
          key: 'name',
          label: 'StorageClass 名称',
          type: 'text',
          placeholder: 'my-storageclass',
          default: 'my-storageclass',
          required: true,
          tip: 'StorageClass 资源名称',
        },
        {
          key: 'provisioner',
          label: 'Provisioner',
          type: 'select',
          options: [
            { label: 'kubernetes.io/aws-ebs', value: 'kubernetes.io/aws-ebs' },
            { label: 'kubernetes.io/gce-pd', value: 'kubernetes.io/gce-pd' },
            { label: 'kubernetes.io/no-provisioner', value: 'kubernetes.io/no-provisioner' },
            { label: 'rancher.io/local-path', value: 'rancher.io/local-path' },
          ],
          default: 'kubernetes.io/aws-ebs',
          required: true,
          tip: '存储卷供应器',
        },
        {
          key: 'reclaimPolicy',
          label: '回收策略',
          type: 'select',
          options: [
            { label: 'Delete', value: 'Delete' },
            { label: 'Retain', value: 'Retain' },
          ],
          default: 'Delete',
        },
        {
          key: 'volumeBindingMode',
          label: '卷绑定模式',
          type: 'select',
          options: [
            { label: 'Immediate', value: 'Immediate' },
            { label: 'WaitForFirstConsumer', value: 'WaitForFirstConsumer' },
          ],
          default: 'Immediate',
          tip: 'Immediate 立即绑定，WaitForFirstConsumer 延迟到 Pod 调度时',
        },
        {
          key: 'allowVolumeExpansion',
          label: '允许卷扩展',
          type: 'switch',
          default: true,
          tip: '允许 PVC 扩容',
        },
      ],
    },
    {
      title: 'Provisioner 参数',
      fields: [
        {
          key: 'paramType',
          label: '卷类型',
          type: 'select',
          options: [
            { label: 'gp2', value: 'gp2' },
            { label: 'premium-rs', value: 'premium-rs' },
          ],
          default: 'gp2',
          tip: '存储卷类型 (AWS gp2 / GCE premium-rs)',
        },
        {
          key: 'paramFsType',
          label: '文件系统类型',
          type: 'select',
          options: [
            { label: 'ext4', value: 'ext4' },
            { label: 'xfs', value: 'xfs' },
          ],
          default: 'ext4',
        },
        {
          key: 'paramIopsPerGB',
          label: 'IOPS per GB',
          type: 'text',
          placeholder: '（可选）',
          default: '',
          tip: '每 GB 的 IOPS 配置，留空则不设置',
        },
      ],
    },
  ],
}

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 generateK8sStorageClassYaml(config) {
  const parameters = {
    type: config.paramType,
    fsType: config.paramFsType,
  }
  if (config.paramIopsPerGB) {
    parameters.iopsPerGB = config.paramIopsPerGB
  }

  const storageClass = {
    apiVersion: 'storage.k8s.io/v1',
    kind: 'StorageClass',
    metadata: {
      name: config.name,
      annotations: {
        'storageclass.kubernetes.io/is-default-class': 'false',
      },
    },
    provisioner: config.provisioner,
    reclaimPolicy: config.reclaimPolicy,
    volumeBindingMode: config.volumeBindingMode,
    allowVolumeExpansion: config.allowVolumeExpansion,
    parameters,
  }

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

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