export const prometheusSchema = {
  id: 'prometheus',
  name: 'Prometheus',
  icon: 'TrendCharts',
  category: '日志监控',
  description: '开源监控与告警系统，时间序列数据库',
  format: 'yml',
  fileName: 'prometheus.yml',
  groups: [
    {
      title: '全局配置',
      fields: [
        {
          key: 'globalScrapeInterval',
          label: '采集间隔 (秒)',
          type: 'number',
          min: 5,
          max: 300,
          default: 15,
          tip: '全局数据采集频率',
        },
        {
          key: 'globalEvaluationInterval',
          label: '规则评估间隔 (秒)',
          type: 'number',
          min: 5,
          max: 300,
          default: 15,
          tip: '告警规则评估频率',
        },
        {
          key: 'retentionTime',
          label: '数据保留时间',
          type: 'text',
          default: '15d',
          tip: '如 15d、30d、1y',
        },
        {
          key: 'retentionSize',
          label: '数据保留大小',
          type: 'text',
          default: '0',
          tip: '0=不限制，如 10GB',
        },
      ],
    },
    {
      title: '外部标签',
      fields: [
        {
          key: 'externalLabelsEnv',
          label: '环境标签',
          type: 'text',
          default: 'production',
          tip: '标识部署环境',
        },
        {
          key: 'externalLabelsCluster',
          label: '集群标签',
          type: 'text',
          default: 'k8s-prod',
          tip: '标识所属集群',
        },
      ],
    },
    {
      title: 'Alertmanager',
      fields: [
        {
          key: 'enableAlertmanager',
          label: '启用 Alertmanager',
          type: 'switch',
          default: false,
        },
        {
          key: 'alertmanagerTargets',
          label: 'Alertmanager 地址',
          type: 'text',
          default: 'localhost:9093',
          tip: '多个地址用逗号分隔',
          dependsOn: { key: 'enableAlertmanager', value: true },
        },
      ],
    },
    {
      title: '采集任务',
      fields: [
        {
          key: 'scrapeJobName',
          label: 'Prometheus Job 名称',
          type: 'text',
          default: 'prometheus',
        },
        {
          key: 'scrapeTarget',
          label: 'Prometheus 自监控地址',
          type: 'text',
          default: 'localhost:9090',
        },
        {
          key: 'enableNodeExporter',
          label: '启用 Node Exporter',
          type: 'switch',
          default: true,
          tip: '采集主机指标（CPU、内存、磁盘等）',
        },
        {
          key: 'nodeExporterTarget',
          label: 'Node Exporter 地址',
          type: 'text',
          default: 'localhost:9100',
          dependsOn: { key: 'enableNodeExporter', value: true },
        },
      ],
    },
    {
      title: '远程读写',
      fields: [
        {
          key: 'enableRemoteWrite',
          label: '启用 Remote Write',
          type: 'switch',
          default: false,
          tip: '将数据写入远程存储',
        },
        {
          key: 'remoteWriteUrl',
          label: 'Remote Write URL',
          type: 'text',
          default: 'http://localhost:8428/api/v1/write',
          dependsOn: { key: 'enableRemoteWrite', value: true },
        },
        {
          key: 'enableRemoteRead',
          label: '启用 Remote Read',
          type: 'switch',
          default: false,
          tip: '从远程存储读取数据',
        },
        {
          key: 'remoteReadUrl',
          label: 'Remote Read URL',
          type: 'text',
          default: '',
          dependsOn: { key: 'enableRemoteRead', value: true },
        },
      ],
    },
    {
      title: 'TLS 安全',
      fields: [
        {
          key: 'enableTls',
          label: '启用 TLS',
          type: 'switch',
          default: false,
        },
      ],
    },
  ],
}

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

  // Global
  lines.push(`# ======================== 全局配置 ========================`)
  lines.push(`global:`)
  lines.push(`  scrape_interval: ${config.globalScrapeInterval}s`)
  lines.push(`  evaluation_interval: ${config.globalEvaluationInterval}s`)
  lines.push(`  external_labels:`)
  lines.push(`    env: "${config.externalLabelsEnv}"`)
  lines.push(`    cluster: "${config.externalLabelsCluster}"`)
  lines.push(``)

  // Alerting
  if (config.enableAlertmanager) {
    lines.push(`# ======================== Alertmanager ========================`)
    lines.push(`alerting:`)
    lines.push(`  alertmanagers:`)
    lines.push(`    - static_configs:`)
    lines.push(`        - targets:`)
    const targets = config.alertmanagerTargets.split(',').map(t => t.trim())
    targets.forEach(t => {
      lines.push(`          - '${t}'`)
    })
    lines.push(``)
  }

  // Rules
  lines.push(`# ======================== 规则文件 ========================`)
  lines.push(`rule_files:`)
  lines.push(`  - "rules/*.yml"`)
  lines.push(``)

  // Scrape
  lines.push(`# ======================== 采集任务 ========================`)
  lines.push(`scrape_configs:`)
  lines.push(``)

  // Prometheus self-monitoring
  lines.push(`  - job_name: '${config.scrapeJobName}'`)
  lines.push(`    static_configs:`)
  lines.push(`      - targets: ['${config.scrapeTarget}']`)
  lines.push(``)

  // Node Exporter
  if (config.enableNodeExporter) {
    lines.push(`  - job_name: 'node-exporter'`)
    lines.push(`    static_configs:`)
    lines.push(`      - targets: ['${config.nodeExporterTarget}']`)
    lines.push(``)
  }

  // Remote Write
  if (config.enableRemoteWrite) {
    lines.push(`# ======================== Remote Write ========================`)
    lines.push(`remote_write:`)
    lines.push(`  - url: "${config.remoteWriteUrl}"`)
    lines.push(``)
  }

  // Remote Read
  if (config.enableRemoteRead && config.remoteReadUrl) {
    lines.push(`# ======================== Remote Read ========================`)
    lines.push(`remote_read:`)
    lines.push(`  - url: "${config.remoteReadUrl}"`)
    lines.push(``)
  }

  // Storage (as comments, since these are usually CLI args)
  lines.push(`# ======================== 存储设置 (命令行参数) ========================`)
  lines.push(`# --storage.tsdb.retention.time=${config.retentionTime}`)
  if (config.retentionSize !== '0') {
    lines.push(`# --storage.tsdb.retention.size=${config.retentionSize}`)
  }
  if (config.enableTls) {
    lines.push(`# --web.config.file=web.yml  # 配置 TLS`)
  }

  return lines.join('\n')
}
