const { contextBridge, ipcRenderer } = require('electron');

// 暴露安全的 API 给渲染进程
contextBridge.exposeInMainWorld('electronAPI', {
  // 数据库操作
  dbQuery: (params) => ipcRenderer.invoke('db-query', params),
  dbLogin: (params) => ipcRenderer.invoke('db-login', params),

  // 文件操作
  saveDialog: (params) => ipcRenderer.invoke('save-dialog', params),
  openDialog: (params) => ipcRenderer.invoke('open-dialog', params),
  writeFile: (params) => ipcRenderer.invoke('write-file', params),
  readFile: (params) => ipcRenderer.invoke('read-file', params),
  getPaths: () => ipcRenderer.invoke('get-paths'),

  // 通知
  showNotification: (params) => ipcRenderer.invoke('show-notification', params),

  // 云同步接口
  syncSaveExport: (data) => ipcRenderer.invoke('sync-save-export', { data }),
  syncLoadImport: () => ipcRenderer.invoke('sync-load-import'),
  syncGetDir: () => ipcRenderer.invoke('sync-get-dir'),

  // 监听主进程事件
  onMenuAction: (callback) => {
    ipcRenderer.on('menu-export-customers', () => callback('export-customers'));
    ipcRenderer.on('menu-backup', () => callback('backup'));
    ipcRenderer.on('menu-cloud-upload', () => callback('cloud-upload'));
    ipcRenderer.on('menu-cloud-download', () => callback('cloud-download'));
  },

  // 平台标识
  platform: process.platform,
  isElectron: true
});

