export const sambaSchema = {
  id: 'samba',
  name: 'SAMBA',
  icon: 'Files',
  category: '系统服务',
  description: 'Samba 文件共享服务配置',
  format: 'conf',
  fileName: 'smb.conf',
  groups: [
    {
      title: '全局设置',
      fields: [
        {
          key: 'workgroup',
          label: '工作组',
          type: 'text',
          default: 'WORKGROUP',
          tip: 'Windows 工作组名称',
        },
        {
          key: 'serverString',
          label: '服务器描述',
          type: 'text',
          default: 'Samba Server',
          tip: '服务器的描述字符串',
        },
        {
          key: 'netbiosName',
          label: 'NetBIOS 名称',
          type: 'text',
          default: 'smbserver',
          tip: '网络中显示的计算机名',
        },
        {
          key: 'securityType',
          label: '安全模式',
          type: 'select',
          options: [
            { label: 'user - 用户级认证 (推荐)', value: 'user' },
            { label: 'share - 共享级认证', value: 'share' },
            { label: 'domain - 域认证', value: 'domain' },
          ],
          default: 'user',
        },
        {
          key: 'mapToGuest',
          label: '访客映射',
          type: 'text',
          default: 'Bad User',
          tip: '认证失败时映射为 guest 用户',
        },
      ],
    },
    {
      title: '共享目录',
      fields: [
        {
          key: 'enableShare',
          label: '启用共享',
          type: 'switch',
          default: true,
          tip: '配置一个共享目录',
        },
        {
          key: 'shareName',
          label: '共享名称',
          type: 'text',
          default: 'share',
          dependsOn: { key: 'enableShare', value: true },
          tip: '网络访问路径中的共享名',
        },
        {
          key: 'sharePath',
          label: '共享路径',
          type: 'text',
          default: '/data/samba',
          dependsOn: { key: 'enableShare', value: true },
        },
        {
          key: 'shareComment',
          label: '共享注释',
          type: 'text',
          default: 'Shared Folder',
          dependsOn: { key: 'enableShare', value: true },
        },
        {
          key: 'browseable',
          label: '可浏览',
          type: 'switch',
          default: true,
          dependsOn: { key: 'enableShare', value: true },
          tip: '是否在网络中可见',
        },
        {
          key: 'writable',
          label: '可写',
          type: 'switch',
          default: true,
          dependsOn: { key: 'enableShare', value: true },
        },
        {
          key: 'guestOk',
          label: '允许匿名访问',
          type: 'switch',
          default: false,
          dependsOn: { key: 'enableShare', value: true },
        },
        {
          key: 'validUsers',
          label: '有效用户',
          type: 'text',
          default: 'user1 user2',
          dependsOn: { key: 'enableShare', value: true },
          tip: '允许访问的用户列表，空格分隔',
        },
      ],
    },
    {
      title: '权限掩码',
      fields: [
        {
          key: 'createMask',
          label: '文件创建掩码',
          type: 'text',
          default: '0664',
          tip: '新建文件的权限掩码',
        },
        {
          key: 'directoryMask',
          label: '目录创建掩码',
          type: 'text',
          default: '0775',
          tip: '新建目录的权限掩码',
        },
      ],
    },
    {
      title: '日志配置',
      fields: [
        {
          key: 'enableLogging',
          label: '启用日志',
          type: 'switch',
          default: true,
        },
        {
          key: 'logFile',
          label: '日志文件',
          type: 'text',
          default: '/var/log/samba/log.%m',
          dependsOn: { key: 'enableLogging', value: true },
          tip: '%m 会被替换为客户端机器名',
        },
        {
          key: 'maxLogSize',
          label: '最大日志大小 (KB)',
          type: 'number',
          min: 0,
          max: 100000,
          default: 5000,
          dependsOn: { key: 'enableLogging', value: true },
          tip: '0 表示不限制',
        },
      ],
    },
  ],
}

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

  // Global section
  lines.push(`# ======================== 全局设置 ========================`)
  lines.push(`[global]`)
  lines.push(`   workgroup = ${config.workgroup}`)
  lines.push(`   server string = ${config.serverString}`)
  lines.push(`   netbios name = ${config.netbiosName}`)
  lines.push(`   security = ${config.securityType}`)
  lines.push(`   map to guest = ${config.mapToGuest}`)
  lines.push(``)

  // Logging
  if (config.enableLogging) {
    lines.push(`   # 日志配置`)
    lines.push(`   log file = ${config.logFile}`)
    if (config.maxLogSize > 0) {
      lines.push(`   max log size = ${config.maxLogSize}`)
    }
    lines.push(``)
  }

  // Performance tuning
  lines.push(`   # 性能优化`)
  lines.push(`   socket options = TCP_NODELAY IPTOS_LOWDELAY`)
  lines.push(`   load printers = no`)
  lines.push(`   printing = bsd`)
  lines.push(`   printcap name = /dev/null`)
  lines.push(`   disable spoolss = yes`)
  lines.push(``)

  // Share section
  if (config.enableShare) {
    lines.push(`# ======================== 共享目录 ========================`)
    lines.push(`[${config.shareName}]`)
    lines.push(`   comment = ${config.shareComment}`)
    lines.push(`   path = ${config.sharePath}`)
    lines.push(`   browseable = ${config.browseable ? 'yes' : 'no'}`)
    lines.push(`   writable = ${config.writable ? 'yes' : 'no'}`)
    lines.push(`   guest ok = ${config.guestOk ? 'yes' : 'no'}`)

    if (!config.guestOk && config.validUsers) {
      const users = config.validUsers.split(/\s+/).filter(Boolean).join(', ')
      lines.push(`   valid users = ${users}`)
    }

    lines.push(`   create mask = ${config.createMask}`)
    lines.push(`   directory mask = ${config.directoryMask}`)
    lines.push(``)
  }

  // Tips
  lines.push(`# ======================== 使用提示 ========================`)
  lines.push(`# 1. 添加 Samba 用户: smbpasswd -a <username>`)
  lines.push(`# 2. 确保目录存在: mkdir -p ${config.enableShare ? config.sharePath : '/data/samba'}`)
  lines.push(`# 3. 设置权限: chmod 775 ${config.enableShare ? config.sharePath : '/data/samba'}`)
  lines.push(`# 4. 重启服务: systemctl restart smb nmb`)
  lines.push(`# 5. 测试配置: testparm`)

  return lines.join('\n')
}
