export const gitlabSchema = {
  id: 'gitlab',
  name: 'GitLab',
  icon: 'Files',
  category: 'CI/CD',
  description: 'DevOps 生命周期平台，提供代码托管、CI/CD、制品管理等',
  format: 'rb',
  fileName: 'gitlab.rb',
  groups: [
    {
      title: '基础配置',
      fields: [
        {
          key: 'externalUrl',
          label: '外部访问 URL',
          type: 'text',
          placeholder: 'http://gitlab.example.com',
          default: 'http://gitlab.example.com',
          required: true,
          tip: 'GitLab 实例的外部访问地址',
        },
      ],
    },
    {
      title: 'Nginx 配置',
      fields: [
        {
          key: 'nginxListenPort',
          label: 'Nginx 监听端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 80,
        },
        {
          key: 'nginxEnableSsl',
          label: '启用 SSL',
          type: 'switch',
          default: false,
          tip: '为 Nginx 启用 HTTPS',
        },
        {
          key: 'sslCertificate',
          label: 'SSL 证书路径',
          type: 'text',
          default: '/etc/gitlab/ssl/gitlab.crt',
          dependsOn: { key: 'nginxEnableSsl', value: true },
          required: true,
        },
        {
          key: 'sslCertificateKey',
          label: 'SSL 私钥路径',
          type: 'text',
          default: '/etc/gitlab/ssl/gitlab.key',
          dependsOn: { key: 'nginxEnableSsl', value: true },
          required: true,
        },
      ],
    },
    {
      title: '数据库配置',
      fields: [
        {
          key: 'gitlabRbDbAdapter',
          label: '数据库类型',
          type: 'select',
          options: [
            { label: 'PostgreSQL', value: 'postgresql' },
            { label: 'MySQL', value: 'mysql' },
          ],
          default: 'postgresql',
        },
        {
          key: 'gitlabRbDbHost',
          label: '数据库主机',
          type: 'text',
          default: '127.0.0.1',
        },
        {
          key: 'gitlabRbDbPort',
          label: '数据库端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 5432,
        },
        {
          key: 'gitlabRbDbName',
          label: '数据库名称',
          type: 'text',
          default: 'gitlabhq_production',
        },
        {
          key: 'gitlabRbDbUser',
          label: '数据库用户',
          type: 'text',
          default: 'gitlab',
        },
        {
          key: 'gitlabRbDbPassword',
          label: '数据库密码',
          type: 'text',
          default: 'changeme',
          tip: '生产环境请使用强密码',
        },
      ],
    },
    {
      title: 'Redis 配置',
      fields: [
        {
          key: 'redisHost',
          label: 'Redis 主机',
          type: 'text',
          default: '127.0.0.1',
        },
        {
          key: 'redisPort',
          label: 'Redis 端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 6379,
        },
      ],
    },
    {
      title: 'Container Registry',
      fields: [
        {
          key: 'registryEnable',
          label: '启用 Container Registry',
          type: 'switch',
          default: false,
          tip: 'Docker 镜像仓库',
        },
        {
          key: 'registryExternalUrl',
          label: 'Registry 外部 URL',
          type: 'text',
          placeholder: 'http://registry.example.com',
          default: 'http://registry.example.com',
          dependsOn: { key: 'registryEnable', value: true },
        },
      ],
    },
    {
      title: 'GitLab Pages',
      fields: [
        {
          key: 'pagesEnable',
          label: '启用 GitLab Pages',
          type: 'switch',
          default: false,
          tip: '静态网站托管服务',
        },
        {
          key: 'pagesExternalUrl',
          label: 'Pages 外部 URL',
          type: 'text',
          placeholder: 'http://pages.example.com',
          default: 'http://pages.example.com',
          dependsOn: { key: 'pagesEnable', value: true },
        },
      ],
    },
    {
      title: 'SMTP 邮件配置',
      fields: [
        {
          key: 'smtpEnable',
          label: '启用 SMTP',
          type: 'switch',
          default: false,
          tip: '用于发送邮件通知',
        },
        {
          key: 'smtpAddress',
          label: 'SMTP 服务器',
          type: 'text',
          default: 'smtp.example.com',
          dependsOn: { key: 'smtpEnable', value: true },
        },
        {
          key: 'smtpPort',
          label: 'SMTP 端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 587,
          dependsOn: { key: 'smtpEnable', value: true },
        },
        {
          key: 'smtpUser',
          label: 'SMTP 用户名',
          type: 'text',
          default: 'gitlab@example.com',
          dependsOn: { key: 'smtpEnable', value: true },
        },
        {
          key: 'smtpPassword',
          label: 'SMTP 密码',
          type: 'text',
          default: 'changeme',
          dependsOn: { key: 'smtpEnable', value: true },
        },
        {
          key: 'smtpTls',
          label: '启用 TLS',
          type: 'switch',
          default: true,
          dependsOn: { key: 'smtpEnable', value: true },
        },
      ],
    },
    {
      title: '备份与监控',
      fields: [
        {
          key: 'backupKeepTime',
          label: '备份保留时间 (秒)',
          type: 'number',
          min: 0,
          default: 604800,
          tip: '604800 秒 = 7 天',
        },
        {
          key: 'prometheusEnable',
          label: '启用 Prometheus 监控',
          type: 'switch',
          default: true,
        },
      ],
    },
  ],
}

export function generateGitlabRb(config) {
  const lines = []
  lines.push(`# GitLab 配置文件 - 由 ConfTemplate 生成`)
  lines.push(`# 生成时间: ${new Date().toLocaleString('zh-CN')}`)
  lines.push(``)
  lines.push(`## 外部访问 URL`)
  lines.push(`external_url '${config.externalUrl}'`)
  lines.push(``)

  // Nginx
  lines.push(`## Nginx 配置`)
  lines.push(`nginx['listen_port'] = ${config.nginxListenPort}`)
  if (config.nginxEnableSsl) {
    lines.push(`nginx['ssl_certificate'] = '${config.sslCertificate}'`)
    lines.push(`nginx['ssl_certificate_key'] = '${config.sslCertificateKey}'`)
  } else {
    lines.push(`nginx['enable_ssl'] = false`)
  }
  lines.push(``)

  // Database
  lines.push(`## 数据库配置`)
  if (config.gitlabRbDbAdapter === 'postgresql') {
    lines.push(`postgresql['enable'] = true`)
    lines.push(`gitlab_rails['db_adapter'] = 'postgresql'`)
    lines.push(`gitlab_rails['db_host'] = '${config.gitlabRbDbHost}'`)
    lines.push(`gitlab_rails['db_port'] = ${config.gitlabRbDbPort}`)
  } else {
    lines.push(`postgresql['enable'] = false`)
    lines.push(`gitlab_rails['db_adapter'] = 'mysql2'`)
    lines.push(`gitlab_rails['db_host'] = '${config.gitlabRbDbHost}'`)
    lines.push(`gitlab_rails['db_port'] = ${config.gitlabRbDbPort}`)
  }
  lines.push(`gitlab_rails['db_database'] = '${config.gitlabRbDbName}'`)
  lines.push(`gitlab_rails['db_username'] = '${config.gitlabRbDbUser}'`)
  lines.push(`gitlab_rails['db_password'] = '${config.gitlabRbDbPassword}'`)
  lines.push(``)

  // Redis
  lines.push(`## Redis 配置`)
  lines.push(`redis['enable'] = true`)
  lines.push(`gitlab_rails['redis_host'] = '${config.redisHost}'`)
  lines.push(`gitlab_rails['redis_port'] = ${config.redisPort}`)
  lines.push(``)

  // Registry
  if (config.registryEnable) {
    lines.push(`## Container Registry`)
    lines.push(`registry['enable'] = true`)
    lines.push(`registry_external_url '${config.registryExternalUrl}'`)
    lines.push(``)
  }

  // Pages
  if (config.pagesEnable) {
    lines.push(`## GitLab Pages`)
    lines.push(`pages_external_url '${config.pagesExternalUrl}'`)
    lines.push(`gitlab_pages['enable'] = true`)
    lines.push(``)
  }

  // SMTP
  if (config.smtpEnable) {
    lines.push(`## SMTP 邮件配置`)
    lines.push(`gitlab_rails['smtp_enable'] = true`)
    lines.push(`gitlab_rails['smtp_address'] = '${config.smtpAddress}'`)
    lines.push(`gitlab_rails['smtp_port'] = ${config.smtpPort}`)
    lines.push(`gitlab_rails['smtp_user_name'] = '${config.smtpUser}'`)
    lines.push(`gitlab_rails['smtp_password'] = '${config.smtpPassword}'`)
    lines.push(`gitlab_rails['smtp_tls'] = ${config.smtpTls}`)
    lines.push(``)
  }

  // Backup
  lines.push(`## 备份配置`)
  lines.push(`gitlab_rails['backup_keep_time'] = ${config.backupKeepTime}`)
  lines.push(``)

  // Prometheus
  if (config.prometheusEnable) {
    lines.push(`## Prometheus 监控`)
    lines.push(`prometheus['enable'] = true`)
  } else {
    lines.push(`prometheus['enable'] = false`)
  }

  return lines.join('\n')
}
