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

contextBridge.exposeInMainWorld('windowsAppBridge', {
  saveApiKey: (token) => ipcRenderer.invoke('save-api-key', token),
  persistApp: (app) => ipcRenderer.invoke('persist-app', app),
  listApps: () => ipcRenderer.invoke('list-apps'),
  syncApp: (appId) => ipcRenderer.invoke('sync-app', appId),
  runOpencodeTask: (appId, taskName, args) => 
    ipcRenderer.invoke('run-opencode-task', appId, taskName, args || []),
  
  getVersion: () => ipcRenderer.invoke('get-version'),
  checkForUpdates: () => ipcRenderer.invoke('check-for-updates'),
  downloadUpdate: () => ipcRenderer.invoke('download-update'),
  installUpdate: () => ipcRenderer.invoke('install-update'),
  
  onUpdateAvailable: (callback) => {
    ipcRenderer.on('update-available', () => callback());
  },
  onUpdateDownloaded: (callback) => {
    ipcRenderer.on('update-downloaded', () => callback());
  },
  onUpdateError: (callback) => {
    ipcRenderer.on('update-error', (event, error) => callback(error));
  },
  onDownloadProgress: (callback) => {
    ipcRenderer.on('download-progress', (event, progress) => callback(progress));
  },
  onAppVersion: (callback) => {
    ipcRenderer.on('app-version', (event, version) => callback(version));
  },
});
