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

// Expose protected methods that allow the renderer process to use
// the ipcRenderer without exposing the entire object
contextBridge.exposeInMainWorld('electronAPI', {
  // Platform detection
  checkPlatform: () => ipcRenderer.invoke('check-platform'),
  
  // Dependency checking
  checkDependencies: (options) => ipcRenderer.invoke('check-dependencies', options),
  checkDocker: () => ipcRenderer.invoke('check-docker'),
  checkCaddy: (options) => ipcRenderer.invoke('check-caddy', options),
  installDocker: () => ipcRenderer.invoke('install-docker'),
  installCaddy: (options) => ipcRenderer.invoke('install-caddy', options),
  
  // Configuration
  validatePath: (path) => ipcRenderer.invoke('validate-path', path),
  selectFolder: () => ipcRenderer.invoke('select-folder'),
  saveConfig: (config) => ipcRenderer.invoke('save-config', config),
  loadConfig: (path) => ipcRenderer.invoke('load-config', path),
  
  // File deployment
  validateSources: () => ipcRenderer.invoke('validate-sources'),
  deployDashboard: (installPath) => ipcRenderer.invoke('deploy-dashboard', installPath),
  deployAPI: (installPath) => ipcRenderer.invoke('deploy-api', installPath),
  deployComplete: (installPath) => ipcRenderer.invoke('deploy-complete', installPath),
  
  // Caddyfile generation
  createCaddyfile: (installPath, options) => ipcRenderer.invoke('create-caddyfile', installPath, options),
  createDockerCompose: (installPath) => ipcRenderer.invoke('create-docker-compose', installPath),
  
  // Browser launcher
  openDashboard: (port, hostname) => ipcRenderer.invoke('open-dashboard', port, hostname),
  
  // Installation
  runInstallation: (config) => ipcRenderer.invoke('run-installation', config),
  
  // DNS
  testDNSConnection: (credentials) => ipcRenderer.invoke('test-dns-connection', credentials),

  // Network detection
  detectNetwork: () => ipcRenderer.invoke('detect-network'),

  // Health check
  healthCheck: (config) => ipcRenderer.invoke('health-check', config),

  // Uninstallation
  detectInstallation: () => ipcRenderer.invoke('detect-installation'),
  checkPreservedSettings: (installPath) => ipcRenderer.invoke('check-preserved-settings', installPath),
  restorePreservedSettings: (installPath) => ipcRenderer.invoke('restore-preserved-settings', installPath),
  runUninstallation: (options) => ipcRenderer.invoke('run-uninstallation', options),

  // Service management
  startServices: (config) => ipcRenderer.invoke('start-services', config),
  stopServices: (config) => ipcRenderer.invoke('stop-services', config),
  checkServiceStatus: (config) => ipcRenderer.invoke('check-service-status', config),

  // DNS credentials
  saveDNSCredentials: (credentials, installPath) =>
    ipcRenderer.invoke('save-dns-credentials', credentials, installPath),

  // Branding
  saveBranding: (branding, installPath) =>
    ipcRenderer.invoke('save-branding', branding, installPath),

  // File selection (for logo, etc.)
  selectFile: (options) => ipcRenderer.invoke('select-file', options),

  // Event listeners
  onStepProgress: (callback) => {
    ipcRenderer.on('step-progress', (event, data) => callback(data));
  },
  onStepComplete: (callback) => {
    ipcRenderer.on('step-complete', (event, data) => callback(data));
  },
  onStepError: (callback) => {
    ipcRenderer.on('step-error', (event, data) => callback(data));
  },
  onInstallationComplete: (callback) => {
    ipcRenderer.on('installation-complete', (event, data) => callback(data));
  },
  onDeploymentProgress: (callback) => {
    ipcRenderer.on('deployment-progress', (event, data) => callback(data));
  },
  onUninstallProgress: (callback) => {
    ipcRenderer.on('uninstall-progress', (event, data) => callback(data));
  },
  onUninstallStepComplete: (callback) => {
    ipcRenderer.on('uninstall-step-complete', (event, data) => callback(data));
  },
  onUninstallComplete: (callback) => {
    ipcRenderer.on('uninstall-complete', (event, data) => callback(data));
  },
  onUninstallError: (callback) => {
    ipcRenderer.on('uninstall-error', (event, data) => callback(data));
  },
  
  // Remove listeners
  removeListener: (channel) => {
    ipcRenderer.removeAllListeners(channel);
  }
});
