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', {
  // Notification handling
  showNotification: (title, body) => ipcRenderer.invoke('show-notification', title, body),
  
  // File operations
  saveFile: (data, filename) => ipcRenderer.invoke('save-file', data, filename),
  openFile: () => ipcRenderer.invoke('open-file'),
  
  // Theme handling
  onThemeChange: (callback) => ipcRenderer.on('theme-changed', callback),
  
  // Data export/import
  exportData: () => ipcRenderer.invoke('export-data'),
  importData: () => ipcRenderer.invoke('import-data'),
  
  // App info
  getVersion: () => process.env.npm_package_version || '1.0.0',
  getPlatform: () => process.platform
});

// Expose localStorage operations for persistence
contextBridge.exposeInMainWorld('storage', {
  get: (key) => localStorage.getItem(key),
  set: (key, value) => localStorage.setItem(key, value),
  remove: (key) => localStorage.removeItem(key),
  clear: () => localStorage.clear(),
  getAll: () => {
    const data = {};
    for (let i = 0; i < localStorage.length; i++) {
      const key = localStorage.key(i);
      data[key] = localStorage.getItem(key);
    }
    return data;
  }
});

// Windows 11 specific API
contextBridge.exposeInMainWorld('windowsAPI', {
  // Check if running on Windows
  isWindows: () => process.platform === 'win32',
  
  // Get Windows version info
  getWindowsVersion: () => {
    if (process.platform === 'win32') {
      return {
        platform: 'win32',
        arch: process.arch,
        version: process.getSystemVersion?.() || 'Unknown'
      };
    }
    return null;
  },
  
  // Check for Windows 11 features
  supportsMica: () => {
    // Check if Windows 11 (build 22000+) for Mica effect
    if (process.platform === 'win32') {
      const version = process.getSystemVersion?.();
      if (version) {
        const buildNumber = parseInt(version.split('.')?.[2] || '0');
        return buildNumber >= 22000;
      }
    }
    return false;
  }
});