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

contextBridge.exposeInMainWorld('api', {
  // Python setup APIs
  checkPythonSetup: () => ipcRenderer.invoke('check-python-setup'),
  runPythonSetup: () => ipcRenderer.invoke('run-python-setup'),
  onSetupProgress: (callback) => {
    // Clear previous listener to prevent leaks
    ipcRenderer.removeAllListeners('setup-progress');
    ipcRenderer.on('setup-progress', (event, data) => callback(data));
  },

  // Processing APIs
  removeBackground: (inputPath, outputPath) => 
    ipcRenderer.invoke('remove-background', { inputPath, outputPath }),
  onProcessProgress: (callback) => {
    ipcRenderer.removeAllListeners('process-progress');
    ipcRenderer.on('process-progress', (event, data) => callback(data));
  },

  // File Operations
  selectImage: () => ipcRenderer.invoke('select-image'),
  getTempPath: (fileName) => ipcRenderer.invoke('get-temp-path', fileName),
  exportImage: (base64Data, defaultName) => 
    ipcRenderer.invoke('export-image', { base64Data, defaultName }),
  showInExplorer: (filePath) => ipcRenderer.invoke('show-in-explorer', filePath),

  // Window Controls
  minimizeWindow: () => ipcRenderer.send('window-minimize'),
  maximizeWindow: () => ipcRenderer.send('window-maximize'),
  closeWindow: () => ipcRenderer.send('window-close')
});
