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

// Import types
import type { LLMConfig, DocumentMetadata } from './types';

interface Directory {
  name: string;
  path: string;
}

interface IpcResponse<T> {
  success: boolean;
  data?: T;
  error?: string;
}

// Expose protected methods that allow the renderer process to use
// the ipcRenderer without exposing the entire object
contextBridge.exposeInMainWorld('electron', {
  // File System Operations
  startWatching: async (dirPath: string): Promise<void> => 
    ipcRenderer.invoke('start-watching', dirPath),
  stopWatching: async (dirPath: string): Promise<void> => 
    ipcRenderer.invoke('stop-watching', dirPath),
  addExcludedPath: async (path: string): Promise<void> => 
    ipcRenderer.invoke('add-excluded-path', path),
  
  // LLM Operations
  queryLLM: async (question: string): Promise<{
    answer: string;
    sources: DocumentMetadata[];
  }> => ipcRenderer.invoke('query-llm', question),
  getLLMConfig: async (): Promise<LLMConfig> => 
    ipcRenderer.invoke('get-llm-config'),
  
  // Vector Store Operations
  getDocuments: async (): Promise<IpcResponse<DocumentMetadata[]>> =>
    ipcRenderer.invoke('get-documents'),
  addDocument: async (content: string, metadata: DocumentMetadata): Promise<void> => 
    ipcRenderer.invoke('add-document', content, metadata),
  deleteDocument: async (path: string): Promise<void> => 
    ipcRenderer.invoke('delete-document', path),
  updateDocument: async (content: string, metadata: DocumentMetadata): Promise<void> => 
    ipcRenderer.invoke('update-document', content, metadata),
  
  // File Processing
  processFile: async (filePath: string): Promise<void> => 
    ipcRenderer.invoke('process-file', filePath),
  
  // System Paths
  getUserHome: async (): Promise<IpcResponse<string>> => 
    ipcRenderer.invoke('get-user-home'),
  getAppPath: async (): Promise<string> => 
    ipcRenderer.invoke('get-app-path'),
  
  // Directory Operations
  listDirectories: async (dirPath: string): Promise<IpcResponse<Directory[]>> => 
    ipcRenderer.invoke('list-directories', dirPath),
    
  // Event Handling
  on: (channel: string, callback: (event: unknown, ...args: any[]) => void) => {
    ipcRenderer.on(channel, callback);
  },
  off: (channel: string, callback: (event: unknown, ...args: any[]) => void) => {
    ipcRenderer.removeListener(channel, callback);
  },

  checkOllama: () => ipcRenderer.invoke('check-ollama'),
  openExternal: (url: string) => ipcRenderer.invoke('open-external', url),
  
  // Model Operations
  checkModel: (modelName: string) => ipcRenderer.invoke('check-model', modelName),
  pullModel: (modelName: string, onProgress: (status: string) => void) => {
    const channel = `pull-model-progress-${modelName}`;
    ipcRenderer.on(channel, (_event, status) => onProgress(status));
    return ipcRenderer.invoke('pull-model', modelName).finally(() => {
      ipcRenderer.removeListener(channel, onProgress);
    });
  },
  
  // Window Controls
  minimizeWindow: () => ipcRenderer.invoke('window-minimize'),
  maximizeWindow: () => ipcRenderer.invoke('window-maximize'),
  closeWindow: () => ipcRenderer.invoke('window-close'),
});

// Export types for TypeScript
export type { Directory, IpcResponse };

// For CommonJS compatibility
if (typeof module !== 'undefined' && module.exports) {
  module.exports = {};
}