export const grafanaSchema = {
  id: 'grafana',
  name: 'Grafana',
  icon: 'DataLine',
  category: '日志监控',
  description: '开源数据可视化与监控仪表盘平台',
  format: 'ini',
  fileName: 'grafana.ini',
  groups: [
    {
      title: 'HTTP 服务',
      fields: [
        {
          key: 'httpPort',
          label: 'HTTP 端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 3000,
        },
        {
          key: 'domain',
          label: '域名',
          type: 'text',
          default: 'localhost',
        },
        {
          key: 'rootUrl',
          label: '根 URL',
          type: 'text',
          default: 'http://localhost:3000/',
          tip: '完整访问地址，含协议和路径',
        },
      ],
    },
    {
      title: '管理员账号',
      fields: [
        {
          key: 'adminUser',
          label: '管理员用户名',
          type: 'text',
          default: 'admin',
        },
        {
          key: 'adminPassword',
          label: '管理员密码',
          type: 'text',
          default: 'admin',
          tip: '生产务必修改',
        },
      ],
    },
    {
      title: '数据库配置',
      fields: [
        {
          key: 'dbType',
          label: '数据库类型',
          type: 'select',
          options: [
            { label: 'SQLite3 - 文件数据库 (默认)', value: 'sqlite3' },
            { label: 'MySQL', value: 'mysql' },
            { label: 'PostgreSQL', value: 'postgres' },
          ],
          default: 'sqlite3',
        },
        {
          key: 'dbHost',
          label: '数据库主机',
          type: 'text',
          default: 'localhost:3306',
          dependsOn: { key: 'dbType', valueNotIn: ['sqlite3'] },
        },
        {
          key: 'dbName',
          label: '数据库名称',
          type: 'text',
          default: 'grafana',
          dependsOn: { key: 'dbType', valueNotIn: ['sqlite3'] },
        },
        {
          key: 'dbUser',
          label: '数据库用户',
          type: 'text',
          default: 'grafana',
          dependsOn: { key: 'dbType', valueNotIn: ['sqlite3'] },
        },
        {
          key: 'dbPassword',
          label: '数据库密码',
          type: 'text',
          default: '',
          dependsOn: { key: 'dbType', valueNotIn: ['sqlite3'] },
        },
      ],
    },
    {
      title: '认证与安全',
      fields: [
        {
          key: 'enableAnonymousAuth',
          label: '匿名访问',
          type: 'switch',
          default: false,
          tip: '允许无需登录访问',
        },
        {
          key: 'enableUserSignUp',
          label: '允许用户注册',
          type: 'switch',
          default: false,
        },
        {
          key: 'enableAlerting',
          label: '启用告警',
          type: 'switch',
          default: true,
        },
      ],
    },
    {
      title: 'SMTP 邮件',
      fields: [
        {
          key: 'smtpEnabled',
          label: '启用 SMTP',
          type: 'switch',
          default: false,
          tip: '用于发送告警邮件',
        },
        {
          key: 'smtpHost',
          label: 'SMTP 服务器',
          type: 'text',
          default: 'localhost:25',
          dependsOn: { key: 'smtpEnabled', value: true },
        },
        {
          key: 'smtpFromAddress',
          label: '发件人地址',
          type: 'text',
          default: 'grafana@localhost',
          dependsOn: { key: 'smtpEnabled', value: true },
        },
      ],
    },
    {
      title: '日志配置',
      fields: [
        {
          key: 'logMode',
          label: '日志输出方式',
          type: 'select',
          options: [
            { label: 'console - 控制台输出', value: 'console' },
            { label: 'file - 输出到文件', value: 'file' },
          ],
          default: 'console',
        },
        {
          key: 'logLevel',
          label: '日志级别',
          type: 'select',
          options: [
            { label: 'INFO', value: 'Info' },
            { label: 'WARN', value: 'Warn' },
            { label: 'DEBUG', value: 'Debug' },
            { label: 'ERROR', value: 'Error' },
          ],
          default: 'Info',
        },
      ],
    },
  ],
}

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

  // HTTP
  lines.push(`[server]`)
  lines.push(`protocol = http`)
  lines.push(`http_port = ${config.httpPort}`)
  lines.push(`domain = ${config.domain}`)
  lines.push(`root_url = ${config.rootUrl}`)
  lines.push(``)

  // Database
  lines.push(`[database]`)
  if (config.dbType === 'sqlite3') {
    lines.push(`type = sqlite3`)
    lines.push(`path = grafana.db`)
  } else {
    lines.push(`type = ${config.dbType}`)
    lines.push(`host = ${config.dbHost}`)
    lines.push(`name = ${config.dbName}`)
    lines.push(`user = ${config.dbUser}`)
    if (config.dbPassword) {
      lines.push(`password = ${config.dbPassword}`)
    }
  }
  lines.push(``)

  // Security
  lines.push(`[security]`)
  lines.push(`admin_user = ${config.adminUser}`)
  lines.push(`admin_password = ${config.adminPassword}`)
  lines.push(``)

  // Auth
  lines.push(`[auth]`)
  lines.push(`disable_login_form = false`)
  lines.push(`disable_signout_menu = false`)
  if (config.enableAnonymousAuth) {
    lines.push(``)
    lines.push(`[auth.anonymous]`)
    lines.push(`enabled = true`)
  }
  if (config.enableUserSignUp) {
    lines.push(``)
    lines.push(`[users]`)
    lines.push(`allow_sign_up = true`)
  }
  lines.push(``)

  // Alerting
  lines.push(`[alerting]`)
  lines.push(`enabled = ${config.enableAlerting}`)
  lines.push(``)

  // SMTP
  if (config.smtpEnabled) {
    lines.push(`[smtp]`)
    lines.push(`enabled = true`)
    lines.push(`host = ${config.smtpHost}`)
    lines.push(`from_address = ${config.smtpFromAddress}`)
    lines.push(``)
  }

  // Logging
  lines.push(`[log]`)
  lines.push(`mode = ${config.logMode}`)
  lines.push(`level = ${config.logLevel}`)

  return lines.join('\n')
}
