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

contextBridge.exposeInMainWorld('harmony', {
  saveServer: (server) => ipcRenderer.invoke('save-server', server),
  getConfig: () => ipcRenderer.invoke('get-config'),
  navigateToServer: () => ipcRenderer.invoke('navigate-to-server'),
  loadPage: (page) => ipcRenderer.invoke('load-page', page),
  tryConnect: () => ipcRenderer.invoke('try-connect'),
  goOffline: () => ipcRenderer.invoke('go-offline'),
  showNotification: (opts) => ipcRenderer.invoke('show-notification', opts),
  closeNotification: (tag) => ipcRenderer.invoke('close-notification', tag),
  doLogin: (data) => ipcRenderer.invoke('do-login', data),
  doRegister: (data) => ipcRenderer.invoke('do-register', data),
  doVerify2fa: (data) => ipcRenderer.invoke('do-verify-2fa', data),
  getDesktopSources: (opts) => ipcRenderer.invoke('get-desktop-sources', opts),
  onUpdateAvailable: (cb) => ipcRenderer.on('update-available', (_e, info) => cb(info)),
  onUpdateProgress: (cb) => ipcRenderer.on('update-progress', (_e, pct) => cb(pct)),
  onUpdateError: (cb) => ipcRenderer.on('update-error', (_e, msg) => cb(msg)),
  applyUpdate: () => ipcRenderer.invoke('apply-update'),
  dismissUpdate: () => ipcRenderer.invoke('dismiss-update'),
  setScreenshareSource: (id) => ipcRenderer.invoke('set-screenshare-source', id),
  startSystemAudioCapture: (opts) => ipcRenderer.invoke('start-system-audio-capture', opts || {}),
  stopSystemAudioCapture: () => ipcRenderer.invoke('stop-system-audio-capture'),
  onSystemAudioData: (cb) => ipcRenderer.on('system-audio-data', (_e, buf) => cb(buf)),
  onSystemAudioFormat: (cb) => ipcRenderer.on('system-audio-format', (_e, info) => cb(info)),
  offSystemAudioData: () => { ipcRenderer.removeAllListeners('system-audio-data'); ipcRenderer.removeAllListeners('system-audio-format'); },
});

// ── Window controls (for custom titlebar) ──
contextBridge.exposeInMainWorld('harmonyChromeControls', {
  minimize: () => ipcRenderer.invoke('window-minimize'),
  maximize: () => ipcRenderer.invoke('window-maximize'),
  close: () => ipcRenderer.invoke('window-close'),
  isMaximized: () => ipcRenderer.invoke('window-is-maximized'),
  onMaximizedChange: (cb) => ipcRenderer.on('window-maximized', (_, val) => cb(val))
});
