export const dockerComposeSchema = {
  id: 'docker-compose',
  name: 'Docker Compose',
  icon: 'Files',
  category: '容器化',
  description: 'Docker Compose 多容器编排配置',
  format: 'yml',
  fileName: 'docker-compose.yml',
  groups: [
    {
      title: '基础配置',
      fields: [
        {
          key: 'version',
          label: 'Compose 版本',
          type: 'select',
          options: [
            { label: '3.8 (推荐)', value: '3.8' },
            { label: '3.7', value: '3.7' },
            { label: '3.5', value: '3.5' },
            { label: '2.4', value: '2.4' },
          ],
          default: '3.8',
        },
        {
          key: 'serviceName',
          label: '服务名称',
          type: 'text',
          default: 'my-app',
        },
        {
          key: 'image',
          label: '镜像',
          type: 'text',
          default: 'nginx:latest',
          placeholder: '例如: nginx:latest, redis:7-alpine',
        },
        {
          key: 'containerName',
          label: '容器名称',
          type: 'text',
          default: 'my-app',
        },
        {
          key: 'restartPolicy',
          label: '重启策略',
          type: 'select',
          options: [
            { label: 'no (不自动重启)', value: 'no' },
            { label: 'always (总是重启)', value: 'always' },
            { label: 'unless-stopped (除非手动停止)', value: 'unless-stopped' },
            { label: 'on-failure (失败时重启)', value: 'on-failure' },
          ],
          default: 'unless-stopped',
        },
      ],
    },
    {
      title: '端口映射',
      fields: [
        {
          key: 'hostPort',
          label: '主机端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 80,
        },
        {
          key: 'containerPort',
          label: '容器端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 80,
        },
      ],
    },
    {
      title: '环境变量',
      fields: [
        {
          key: 'enableEnvVars',
          label: '配置环境变量',
          type: 'switch',
          default: false,
        },
        {
          key: 'envVars',
          label: '环境变量',
          type: 'text',
          default: 'KEY=value',
          placeholder: '每行一个: KEY=value',
          dependsOn: { key: 'enableEnvVars', value: true },
          tip: '每行一个 KEY=value 格式',
        },
      ],
    },
    {
      title: '存储卷',
      fields: [
        {
          key: 'enableVolumes',
          label: '挂载存储卷',
          type: 'switch',
          default: false,
        },
        {
          key: 'volumes',
          label: '卷映射',
          type: 'text',
          default: './data:/data',
          placeholder: '每行一个: host_path:container_path',
          dependsOn: { key: 'enableVolumes', value: true },
          tip: '每行一个，格式: 宿主路径:容器路径',
        },
      ],
    },
    {
      title: '网络配置',
      fields: [
        {
          key: 'enableNetwork',
          label: '自定义网络',
          type: 'switch',
          default: false,
        },
        {
          key: 'networkName',
          label: '网络名称',
          type: 'text',
          default: 'my-network',
          dependsOn: { key: 'enableNetwork', value: true },
        },
      ],
    },
    {
      title: '依赖服务',
      fields: [
        {
          key: 'enableDependsOn',
          label: '配置依赖服务',
          type: 'switch',
          default: false,
        },
        {
          key: 'dependsOnService',
          label: '依赖的服务名',
          type: 'text',
          default: 'db',
          placeholder: '例如: db, redis',
          dependsOn: { key: 'enableDependsOn', value: true },
          tip: '多个用逗号分隔',
        },
      ],
    },
    {
      title: '资源限制',
      fields: [
        {
          key: 'cpuLimit',
          label: 'CPU 限制',
          type: 'text',
          default: '1.0',
          tip: '例如: 0.5, 1.0, 2.0',
        },
        {
          key: 'memoryLimit',
          label: '内存限制',
          type: 'text',
          default: '512M',
          tip: '例如: 256M, 512M, 1G',
        },
      ],
    },
    {
      title: '健康检查',
      fields: [
        {
          key: 'enableHealthcheck',
          label: '开启健康检查',
          type: 'switch',
          default: false,
        },
        {
          key: 'healthcheckCmd',
          label: '检查命令',
          type: 'text',
          default: 'curl -f http://localhost/ || exit 1',
          dependsOn: { key: 'enableHealthcheck', value: true },
        },
        {
          key: 'healthcheckInterval',
          label: '检查间隔',
          type: 'text',
          default: '30s',
          dependsOn: { key: 'enableHealthcheck', value: true },
        },
        {
          key: 'healthcheckTimeout',
          label: '超时时间',
          type: 'text',
          default: '10s',
          dependsOn: { key: 'enableHealthcheck', value: true },
        },
        {
          key: 'healthcheckRetries',
          label: '重试次数',
          type: 'number',
          min: 1,
          max: 10,
          default: 3,
          dependsOn: { key: 'enableHealthcheck', value: true },
        },
      ],
    },
  ],
}

export function generateDockerComposeYml(config) {
  const lines = []
  lines.push(`# Docker Compose 配置文件 - 由 ConfTemplate 生成`)
  lines.push(`# 生成时间: ${new Date().toLocaleString('zh-CN')}`)
  lines.push(``)

  lines.push(`version: '${config.version}'`)
  lines.push(``)
  lines.push(`services:`)
  lines.push(`  ${config.serviceName}:`)
  lines.push(`    image: ${config.image}`)
  lines.push(`    container_name: ${config.containerName}`)
  lines.push(`    restart: ${config.restartPolicy}`)
  lines.push(``)

  // 端口映射
  lines.push(`    ports:`)
  lines.push(`      - "${config.hostPort}:${config.containerPort}"`)
  lines.push(``)

  // 环境变量
  if (config.enableEnvVars && config.envVars) {
    lines.push(`    environment:`)
    const vars = config.envVars.split('\n').map((v) => v.trim()).filter(Boolean)
    for (const v of vars) {
      lines.push(`      - ${v}`)
    }
    lines.push(``)
  }

  // 存储卷
  if (config.enableVolumes && config.volumes) {
    lines.push(`    volumes:`)
    const vols = config.volumes.split('\n').map((v) => v.trim()).filter(Boolean)
    for (const v of vols) {
      lines.push(`      - ${v}`)
    }
    lines.push(``)
  }

  // 依赖服务
  if (config.enableDependsOn && config.dependsOnService) {
    lines.push(`    depends_on:`)
    const deps = config.dependsOnService.split(',').map((d) => d.trim()).filter(Boolean)
    for (const dep of deps) {
      lines.push(`      - ${dep}`)
    }
    lines.push(``)
  }

  // 资源限制
  lines.push(`    deploy:`)
  lines.push(`      resources:`)
  lines.push(`        limits:`)
  lines.push(`          cpus: '${config.cpuLimit}'`)
  lines.push(`          memory: ${config.memoryLimit}`)
  lines.push(``)

  // 健康检查
  if (config.enableHealthcheck) {
    lines.push(`    healthcheck:`)
    lines.push(`      test: ["CMD-SHELL", "${config.healthcheckCmd}"]`)
    lines.push(`      interval: ${config.healthcheckInterval}`)
    lines.push(`      timeout: ${config.healthcheckTimeout}`)
    lines.push(`      retries: ${config.healthcheckRetries}`)
    lines.push(``)
  }

  // 网络配置
  if (config.enableNetwork && config.networkName) {
    lines.push(`    networks:`)
    lines.push(`      - ${config.networkName}`)
    lines.push(``)
    lines.push(`networks:`)
    lines.push(`  ${config.networkName}:`)
    lines.push(`    driver: bridge`)
  }

  return lines.join('\n')
}
