export const jenkinsSchema = {
  id: 'jenkins',
  name: 'Jenkins',
  icon: 'Setting',
  category: 'CI/CD',
  description: '开源自动化服务器，支持持续集成与持续交付',
  format: 'yml',
  fileName: 'jenkins.yaml',
  groups: [
    {
      title: '基础配置',
      fields: [
        {
          key: 'httpPort',
          label: 'HTTP 端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 8080,
        },
        {
          key: 'ajpPort',
          label: 'AJP 端口',
          type: 'number',
          default: -1,
          tip: '-1 表示禁用 AJP',
        },
        {
          key: 'jenkinsHome',
          label: 'JENKINS_HOME 目录',
          type: 'text',
          default: '/var/lib/jenkins',
        },
        {
          key: 'numExecutors',
          label: '执行器数量',
          type: 'number',
          min: 0,
          max: 100,
          default: 2,
          tip: '同时执行的任务数',
        },
        {
          key: 'mode',
          label: '节点模式',
          type: 'select',
          options: [
            { label: 'normal - 正常模式', value: 'normal' },
            { label: 'exclusive - 仅构建指定任务', value: 'exclusive' },
          ],
          default: 'normal',
        },
      ],
    },
    {
      title: '安全配置',
      fields: [
        {
          key: 'securityRealm',
          label: '安全域',
          type: 'text',
          default: 'default',
          tip: '用户认证方式',
        },
        {
          key: 'enableLdap',
          label: '启用 LDAP 认证',
          type: 'switch',
          default: false,
        },
        {
          key: 'ldapServer',
          label: 'LDAP 服务器',
          type: 'text',
          default: 'ldap://ldap.example.com',
          dependsOn: { key: 'enableLdap', value: true },
        },
        {
          key: 'ldapRootDn',
          label: 'LDAP Root DN',
          type: 'text',
          default: 'dc=example,dc=com',
          dependsOn: { key: 'enableLdap', value: true },
        },
        {
          key: 'authorizationStrategy',
          label: '授权策略',
          type: 'select',
          options: [
            { label: '登录用户可以做任何事', value: 'loggedInUsersCanDoAnything' },
            { label: '项目矩阵授权', value: 'projectMatrix' },
          ],
          default: 'loggedInUsersCanDoAnything',
        },
        {
          key: 'adminUser',
          label: '管理员用户名',
          type: 'text',
          default: 'admin',
        },
        {
          key: 'adminPassword',
          label: '管理员密码',
          type: 'text',
          default: 'admin',
          tip: '生产环境请修改默认密码',
        },
      ],
    },
    {
      title: 'JCasC (Configuration as Code)',
      fields: [
        {
          key: 'enableCasc',
          label: '启用 JCasC',
          type: 'switch',
          default: true,
          tip: 'Configuration as Code 插件',
        },
        {
          key: 'jenkinsUrl',
          label: 'Jenkins URL',
          type: 'text',
          default: 'http://localhost:8080/',
          tip: 'Jenkins 系统 URL',
        },
      ],
    },
    {
      title: 'Agent 配置',
      fields: [
        {
          key: 'enableAgent',
          label: '启用 Agent 节点',
          type: 'switch',
          default: false,
          tip: '允许远程 Agent 连接',
        },
        {
          key: 'agentPort',
          label: 'Agent 端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 50000,
          dependsOn: { key: 'enableAgent', value: true },
        },
      ],
    },
    {
      title: 'Git 与 Gitea 集成',
      fields: [
        {
          key: 'enableGitea',
          label: '启用 Gitea 集成',
          type: 'switch',
          default: false,
        },
        {
          key: 'giteaUrl',
          label: 'Gitea 服务器 URL',
          type: 'text',
          default: 'http://gitea.example.com',
          dependsOn: { key: 'enableGitea', value: true },
        },
        {
          key: 'enableGit',
          label: '启用 Git',
          type: 'switch',
          default: true,
        },
        {
          key: 'gitPath',
          label: 'Git 可执行文件路径',
          type: 'text',
          default: '/usr/bin/git',
          dependsOn: { key: 'enableGit', value: true },
        },
      ],
    },
  ],
}

export function generateJenkinsYaml(config) {
  const lines = []
  lines.push(`# Jenkins Configuration as Code - 由 ConfTemplate 生成`)
  lines.push(`# 生成时间: ${new Date().toLocaleString('zh-CN')}`)
  lines.push(``)
  lines.push(`jenkins:`)

  // System
  lines.push(`  systemMessage: "Jenkins configured via ConfTemplate"`)
  lines.push(`  numExecutors: ${config.numExecutors}`)
  lines.push(`  mode: ${config.mode}`)
  lines.push(`  jenkinsHome: "${config.jenkinsHome}"`)
  lines.push(``)

  // Security Realm
  if (config.enableLdap) {
    lines.push(`  securityRealm:`)
    lines.push(`    ldap:`)
    lines.push(`      configurations:`)
    lines.push(`        - server: "${config.ldapServer}"`)
    lines.push(`          rootDN: "${config.ldapRootDn}"`)
  } else {
    lines.push(`  securityRealm:`)
    lines.push(`    local:`)
    lines.push(`      allowsSignup: false`)
    lines.push(`      users:`)
    lines.push(`        - id: "${config.adminUser}"`)
    lines.push(`          password: "${config.adminPassword}"`)
  }
  lines.push(``)

  // Authorization
  if (config.authorizationStrategy === 'loggedInUsersCanDoAnything') {
    lines.push(`  authorizationStrategy:`)
    lines.push(`    loggedInUsersCanDoAnything:`)
    lines.push(`      allowAnonymousRead: false`)
  } else {
    lines.push(`  authorizationStrategy:`)
    lines.push(`    projectMatrix:`)
    lines.push(`      entries:`)
    lines.push(`        - user:"${config.adminUser}":`)
    lines.push(`            permissions:`)
    lines.push(`              - "Overall/Administer"`)
  }
  lines.push(``)

  // Agent
  if (config.enableAgent) {
    lines.push(`  agent:`)
    lines.push(`    port: ${config.agentPort}`)
  }

  // JCasC
  if (config.enableCasc) {
    lines.push(`  configuration-as-code:`)
    lines.push(`    enabled: true`)
  }
  lines.push(``)

  // URL
  lines.push(`unclassified:`)
  lines.push(`  jenkinsLocationConfiguration:`)
  lines.push(`    jenkinsUrl: "${config.jenkinsUrl}"`)
  lines.push(``)

  // Git
  if (config.enableGit) {
    lines.push(`  git:`)
    lines.push(`    installations:`)
    lines.push(`      - name: "Default"`)
    lines.push(`        home: "${config.gitPath}"`)
    lines.push(``)
  }

  // Gitea
  if (config.enableGitea) {
    lines.push(`  giteaServers:`)
    lines.push(`    servers:`)
    lines.push(`      - displayName: "Gitea"`)
    lines.push(`        serverUrl: "${config.giteaUrl}"`)
  }

  return lines.join('\n')
}
