import { contextBridge, ipcRenderer } from "electron";
import type { AgentBridge } from "../shared/bridge";
import type { LogEntry } from "../shared/types";

const bridge: AgentBridge = {
  getConfig: () => ipcRenderer.invoke("config:get"),
  saveConfig: (config) => ipcRenderer.invoke("config:save", config),
  getDashboard: () => ipcRenderer.invoke("dashboard:get"),
  listTasks: () => ipcRenderer.invoke("tasks:list"),
  runTask: (request) => ipcRenderer.invoke("tasks:run", request),
  listLogs: () => ipcRenderer.invoke("logs:list"),
  clearLogs: () => ipcRenderer.invoke("logs:clear"),
  login: (request) => ipcRenderer.invoke("session:login", request),
  logout: () => ipcRenderer.invoke("session:logout"),
  getSession: () => ipcRenderer.invoke("session:get"),
  getCaptcha: () => ipcRenderer.invoke("auth:captcha"),
  getMcpConfig: () => ipcRenderer.invoke("mcp:config:get"),
  chat: (request) => ipcRenderer.invoke("assistant:chat", request),
  extractIntent: (request) => ipcRenderer.invoke("assistant:intent", request),
  runAssistant: (request) => ipcRenderer.invoke("assistant:run", request),
  exportPriceComparison: (request) => ipcRenderer.invoke("exports:price-comparison", request),
  exportTables: (request) => ipcRenderer.invoke("exports:tables", request),
  openExportedFile: (filePath) => ipcRenderer.invoke("exports:open-file", filePath),
  showExportedFile: (filePath) => ipcRenderer.invoke("exports:show-file", filePath),
  onLog: (callback: (entry: LogEntry) => void) => {
    const listener = (_event: Electron.IpcRendererEvent, entry: LogEntry) => callback(entry);
    ipcRenderer.on("logs:entry", listener);
    return () => ipcRenderer.removeListener("logs:entry", listener);
  }
};

contextBridge.exposeInMainWorld("smqjhAgent", bridge);
