import { LogLevel, MachineAction, ElectronAPI } from './types/electron';
import { Machine } from './renderer/types';
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', {
    // Connect to machine
    connectToMachine: (machine: Machine) => 
      ipcRenderer.invoke('connect-to-machine', machine),
    
    // Control machine state
    controlMachine: (machineId: string, action: MachineAction) => 
      ipcRenderer.invoke('control-machine', { machineId, action }),
    
    // Get machine status
    getMachineStatus: (machineId: string) => 
      ipcRenderer.invoke('get-machine-status', machineId),
    
    // Get machine list
    getMachineList: () => 
      ipcRenderer.invoke('get-machine-list'),
    
    // Logging
    logEvent: (level: LogLevel, tag: string, message: string, context?: Record<string, unknown>) => 
      ipcRenderer.send('log-event', { level, tag, message, context }),
    
    // FIXED: Secure storage for JWT tokens (XSS protection)
    encryptData: (plaintext: string) => 
      ipcRenderer.invoke('encrypt-data', plaintext),
    
    decryptData: (encrypted: string) => 
      ipcRenderer.invoke('decrypt-data', encrypted),
    
    // Check safeStorage availability
    isEncryptionAvailable: () => 
      ipcRenderer.invoke('is-encryption-available'),
  } as ElectronAPI
);

// TypeScript types
declare global {
  interface Window {
    electronAPI: ElectronAPI;
  }
} 