export const oracleSchema = {
  id: 'oracle',
  name: 'Oracle',
  icon: 'Coin',
  category: '数据库',
  description: 'Oracle 数据库初始化参数配置',
  format: 'ora',
  fileName: 'init.ora',
  groups: [
    {
      title: '数据库标识',
      fields: [
        {
          key: 'dbName',
          label: '数据库名称',
          type: 'text',
          default: 'ORCL',
        },
        {
          key: 'dbDomain',
          label: '数据库域名',
          type: 'text',
          default: 'localdomain',
        },
        {
          key: 'listenerPort',
          label: '监听端口',
          type: 'number',
          min: 1,
          max: 65535,
          default: 1521,
        },
      ],
    },
    {
      title: '内存配置',
      fields: [
        {
          key: 'sgaMaxSize',
          label: 'SGA 最大大小',
          type: 'select',
          options: [
            { label: '1GB', value: '1GB' },
            { label: '2GB', value: '2GB' },
            { label: '4GB', value: '4GB' },
            { label: '8GB', value: '8GB' },
            { label: '16GB', value: '16GB' },
          ],
          default: '2GB',
          tip: '系统全局区，Oracle 核心内存结构',
        },
        {
          key: 'pgaAggregateTarget',
          label: 'PGA 聚合目标',
          type: 'select',
          options: [
            { label: '256MB', value: '256MB' },
            { label: '512MB', value: '512MB' },
            { label: '1GB', value: '1GB' },
            { label: '2GB', value: '2GB' },
          ],
          default: '512MB',
          tip: '程序全局区，用于排序/哈希等操作',
        },
      ],
    },
    {
      title: '进程与会话',
      fields: [
        {
          key: 'processes',
          label: '最大进程数',
          type: 'number',
          min: 100,
          max: 10000,
          default: 500,
        },
        {
          key: 'sessions',
          label: '最大会话数',
          type: 'number',
          min: 100,
          max: 10000,
          default: 600,
          tip: '通常为 processes 的 1.1-1.2 倍',
        },
        {
          key: 'openCursors',
          label: '最大打开游标数',
          type: 'number',
          min: 100,
          max: 10000,
          default: 300,
        },
      ],
    },
    {
      title: '存储配置',
      fields: [
        {
          key: 'dbBlockSize',
          label: '数据库块大小',
          type: 'select',
          options: [
            { label: '2048 (2KB)', value: '2048' },
            { label: '4096 (4KB)', value: '4096' },
            { label: '8192 (8KB, 推荐)', value: '8192' },
            { label: '16384 (16KB)', value: '16384' },
          ],
          default: '8192',
        },
        {
          key: 'controlFileLocation',
          label: '控制文件路径',
          type: 'text',
          default: '/u01/app/oracle/oradata/',
        },
        {
          key: 'diagnosticDest',
          label: '诊断目录',
          type: 'text',
          default: '/u01/app/oracle',
        },
      ],
    },
    {
      title: '字符集',
      fields: [
        {
          key: 'nlsCharacterSet',
          label: 'NLS 字符集',
          type: 'select',
          options: [
            { label: 'AL32UTF8 (推荐，完整 Unicode)', value: 'AL32UTF8' },
            { label: 'UTF8', value: 'UTF8' },
            { label: 'ZHS16GBK (中文 GBK)', value: 'ZHS16GBK' },
          ],
          default: 'AL32UTF8',
        },
      ],
    },
    {
      title: '归档日志',
      fields: [
        {
          key: 'enableArchiveLog',
          label: '开启归档日志',
          type: 'switch',
          default: false,
          tip: '生产环境必须开启，支持时间点恢复',
        },
        {
          key: 'archiveLogDest',
          label: '归档日志目录',
          type: 'text',
          default: '/u01/app/oracle/archive',
          dependsOn: { key: 'enableArchiveLog', value: true },
        },
      ],
    },
    {
      title: '安全审计',
      fields: [
        {
          key: 'enableAudit',
          label: '开启审计',
          type: 'switch',
          default: true,
        },
      ],
    },
  ],
}

export function generateOracleConf(config) {
  const lines = []
  lines.push(`# Oracle 初始化参数文件 - 由 ConfTemplate 生成`)
  lines.push(`# 生成时间: ${new Date().toLocaleString('zh-CN')}`)
  lines.push(``)

  lines.push(`# ======================== 数据库标识 ========================`)
  lines.push(`db_name='${config.dbName}'`)
  lines.push(`db_domain='${config.dbDomain}'`)
  lines.push(`db_unique_name='${config.dbName}'`)
  lines.push(`instance_name='${config.dbName}'`)
  lines.push(``)

  lines.push(`# ======================== 内存配置 ========================`)
  lines.push(`sga_max_size=${config.sgaMaxSize}`)
  lines.push(`sga_target=${config.sgaMaxSize}`)
  lines.push(`pga_aggregate_target=${config.pgaAggregateTarget}`)
  lines.push(`memory_target=0`)
  lines.push(``)

  lines.push(`# ======================== 进程与会话 ========================`)
  lines.push(`processes=${config.processes}`)
  lines.push(`sessions=${config.sessions}`)
  lines.push(`open_cursors=${config.openCursors}`)
  lines.push(``)

  lines.push(`# ======================== 存储配置 ========================`)
  lines.push(`db_block_size=${config.dbBlockSize}`)
  lines.push(`control_files='${config.controlFileLocation}/control01.ctl','${config.controlFileLocation}/control02.ctl'`)
  lines.push(`diagnostic_dest='${config.diagnosticDest}'`)
  lines.push(``)

  lines.push(`# ======================== 字符集 ========================`)
  lines.push(`nls_language='AMERICAN'`)
  lines.push(`nls_territory='AMERICA'`)
  lines.push(`nls_characterset='${config.nlsCharacterSet}'`)
  lines.push(``)

  lines.push(`# ======================== 网络配置 ========================`)
  lines.push(`dispatchers='(PROTOCOL=TCP) (SERVICE=${config.dbName}XDB)'`)
  lines.push(`local_listener='(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=${config.listenerPort}))'`)
  lines.push(``)

  if (config.enableArchiveLog) {
    lines.push(`# ======================== 归档日志 ========================`)
    lines.push(`log_archive_dest_1='LOCATION=${config.archiveLogDest}'`)
    lines.push(`log_archive_format='%t_%s_%r.arc'`)
    lines.push(`log_archive_start=TRUE`)
    lines.push(``)
  }

  lines.push(`# ======================== 审计配置 ========================`)
  lines.push(`audit_trail=${config.enableAudit ? 'DB' : 'NONE'}`)
  if (config.enableAudit) {
    lines.push(`audit_file_dest='${config.diagnosticDest}/admin/${config.dbName}/adump'`)
  }
  lines.push(``)

  lines.push(`# ======================== 兼容性 ========================`)
  lines.push(`compatible='19.0.0'`)
  lines.push(`undo_tablespace='UNDOTBS1'`)
  lines.push(`undo_management='AUTO'`)

  return lines.join('\n')
}
