console.log('[PRELOAD] Preload script starting');

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

console.log('[PRELOAD] Electron modules imported successfully');

// Expose protected methods that allow the renderer process to use
// the ipcRenderer without exposing the entire object
try {
    contextBridge.exposeInMainWorld('electronAPI', {
        // Window controls
        minimizeWindow: () => ipcRenderer.send('minimize-window'),
        closeWindow: () => ipcRenderer.send('close-window'),
        toggleFullscreen: () => ipcRenderer.send('toggle-fullscreen'),
        
        // Logging
        log: (level, message, data) => ipcRenderer.send('log-message', { level, message, data }),
        
        // Save operations
        createSaveFolders: (saveSlots) => ipcRenderer.invoke('create-save-folders', saveSlots),
        testFileAccess: (slotPath) => ipcRenderer.invoke('test-file-access', slotPath),
        saveGame: (slot, saveData) => ipcRenderer.invoke('save-game', slot, saveData),
        loadGame: (slot) => ipcRenderer.invoke('load-game', slot),
        
        // System operations
        getPath: (name) => ipcRenderer.invoke('get-path', name)
    });

    console.log('[PRELOAD] electronAPI exposed via contextBridge successfully');
} catch (error) {
    console.error('[PRELOAD] Failed to expose electronAPI:', error);
    console.error('[PRELOAD] Error stack:', error.stack);
}
