import { contextBridge, ipcRenderer } from 'electron';
import type { HostedSessionConfig } from '../../../packages/domain/entities/HostedSessionConfig';

export interface ElectronAPI {
  startAutomation: (config: HostedSessionConfig) => Promise<{ success: boolean; sessionId?: string; error?: string }>;
  stopAutomation: (sessionId: string) => Promise<{ success: boolean; error?: string }>;
  getSessionStatus: (sessionId: string) => Promise<any>;
  pauseAutomation: (sessionId: string) => Promise<{ success: boolean; error?: string }>;
  resumeAutomation: (sessionId: string) => Promise<{ success: boolean; error?: string }>;
  onSessionProgress: (callback: (progress: any) => void) => void;
}

contextBridge.exposeInMainWorld('electronAPI', {
  startAutomation: (config: HostedSessionConfig) => ipcRenderer.invoke('start-automation', config),
  stopAutomation: (sessionId: string) => ipcRenderer.invoke('stop-automation', sessionId),
  getSessionStatus: (sessionId: string) => ipcRenderer.invoke('get-session-status', sessionId),
  pauseAutomation: (sessionId: string) => ipcRenderer.invoke('pause-automation', sessionId),
  resumeAutomation: (sessionId: string) => ipcRenderer.invoke('resume-automation', sessionId),
  onSessionProgress: (callback: (progress: any) => void) => {
    ipcRenderer.on('session-progress', (_event, progress) => callback(progress));
  }
} as ElectronAPI);