import { contextBridge, ipcRenderer } from 'electron'

// 暴露安全的 API 给渲染进程
contextBridge.exposeInMainWorld('electronAPI', {
  // 窗口控制
  minimizeWindow: () => ipcRenderer.send('window-minimize'),
  maximizeWindow: () => ipcRenderer.send('window-maximize'),
  closeWindow: () => ipcRenderer.send('window-close'),

  // 应用模式
  isLauncherMode: () => ipcRenderer.invoke('is-launcher-mode'),
  getStartupAction: () => ipcRenderer.invoke('get-startup-action'),

  // 路径获取
  getAppPath: () => ipcRenderer.invoke('get-app-path'),
  getResourcePath: () => ipcRenderer.invoke('get-resource-path'),

  // 安装相关
  selectDirectory: () => ipcRenderer.invoke('select-directory'),
  getDiskSpace: (path: string) => ipcRenderer.invoke('get-disk-space', path),
  checkDependency: (dep: string) => ipcRenderer.invoke('check-dependency', dep),
  installDependency: (dep: string) => ipcRenderer.invoke('install-dependency', dep),
  startInstallation: (config: object) => ipcRenderer.invoke('start-installation', config),
  generateConfigPreview: (params: {
    installDir?: string
    database: { type: 'sqlite' }
    serverPort?: number
  }) => ipcRenderer.invoke('generate-config-preview', params),
  createShortcut: (path: string) => ipcRenderer.invoke('create-shortcut', path),

  // 安装状态
  checkInstalled: () => ipcRenderer.invoke('check-installed'),
  checkOldISDP: () => ipcRenderer.invoke('check-old-isdp'),
  uninstallOldISDP: () => ipcRenderer.invoke('uninstall-old-isdp'),
  readExistingConfig: (installDir: string) => ipcRenderer.invoke('read-existing-config', installDir),

  // 服务管理
  startService: () => ipcRenderer.invoke('start-service'),
  stopService: () => ipcRenderer.invoke('stop-service'),
  getServiceStatus: () => ipcRenderer.invoke('get-service-status'),
  getRunningAgents: () => ipcRenderer.invoke('get-running-agents'),

  // 依赖管理（启动器）
  checkAllDependencies: () => ipcRenderer.invoke('check-all-dependencies'),

  // 配置编辑（启动器）
  readConfigFile: () => ipcRenderer.invoke('read-config-file'),
  getConfigPreview: () => ipcRenderer.invoke('get-config-preview'),
  saveConfig: (yaml: string) => ipcRenderer.invoke('save-config', yaml),
  getExistingConfig: () => ipcRenderer.invoke('get-existing-config'),

  // 快捷操作
  openLogs: () => ipcRenderer.invoke('open-logs'),
  openDataDir: () => ipcRenderer.invoke('open-data-dir'),
  openConfig: () => ipcRenderer.invoke('open-config'),
  openConsole: () => ipcRenderer.invoke('open-console'),

  // 卸载
  confirmUninstall: () => ipcRenderer.invoke('confirm-uninstall'),
  uninstall: (keepData: boolean) => ipcRenderer.invoke('uninstall', keepData),

  // 进度回调
  onInstallProgress: (callback: (progress: any) => void) => {
    ipcRenderer.on('install-progress', (_event, progress) => callback(progress))
  }
})