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

contextBridge.exposeInMainWorld('hyperDesk', {
  sendMessage(threadId, cwd, message, modelName) {
    ipcRenderer.send('pi:send-message', { threadId, cwd, message, modelName })
  },
  chooseFolder(current) {
    ipcRenderer.send('folder:choose', { current })
  },
  chooseFiles(current) {
    ipcRenderer.send('files:choose', { current })
  },
  saveDroppedFiles(files) {
    return ipcRenderer.invoke('files:save-dropped', { files })
  },
  loadSessionState() {
    return ipcRenderer.invoke('sessions:load')
  },
  saveSessionState(value) {
    return ipcRenderer.invoke('sessions:save', { value })
  },
  loadProfile(scope, cwd) {
    return ipcRenderer.invoke('profile:load', { scope, cwd })
  },
  saveProfile(profile) {
    return ipcRenderer.invoke('profile:save', profile)
  },
  loadPiConfig(cwd) {
    return ipcRenderer.invoke('pi-config:load', { cwd })
  },
  savePiConfigKey(cwd, provider, key) {
    return ipcRenderer.invoke('pi-config:save-key', { cwd, provider, key })
  },
  removePiConfigKey(cwd, provider) {
    return ipcRenderer.invoke('pi-config:remove-key', { cwd, provider })
  },
  loginPiConfigOAuth(cwd, provider) {
    return ipcRenderer.invoke('pi-config:oauth-login', { cwd, provider })
  },
  setPiConfigDefault(cwd, provider, model, thinking) {
    return ipcRenderer.invoke('pi-config:set-default', { cwd, provider, model, thinking })
  },
  saveOllamaModel(cwd, model, thinking) {
    return ipcRenderer.invoke('pi-config:save-ollama-model', { cwd, model, thinking })
  },
  abort(threadId) {
    ipcRenderer.send('pi:abort', { threadId })
  },
  requestModels(provider) {
    ipcRenderer.send('pi:list-models', { provider })
  },
  launchChrome() {
    ipcRenderer.send('chrome:launch')
  },
  newChromeTab() {
    ipcRenderer.send('chrome:new-tab')
  },
  refreshChromeTabs() {
    ipcRenderer.send('chrome:list-tabs')
  },
  approve(id) {
    ipcRenderer.send('approval:resolve', { id, approved: true })
  },
  approveAllBrowser(id) {
    ipcRenderer.send('approval:approve-all-browser', { id })
  },
  approveAllLocalEffects(id) {
    ipcRenderer.send('approval:approve-all-local-effects', { id })
  },
  reject(id) {
    ipcRenderer.send('approval:resolve', { id, approved: false })
  },
  openLogin(provider) {
    ipcRenderer.send('pi:open-login', { provider })
  },
  openPath(path) {
    ipcRenderer.send('shell:open-path', { path })
  },
  onSetupRequested(callback) {
    const listener = (_event, event) => callback(event.provider, event.action || 'setup')
    ipcRenderer.on('pi:setup-requested', listener)

    return () => ipcRenderer.removeListener('pi:setup-requested', listener)
  },
  onModels(callback) {
    const listener = (_event, event) => callback(event.provider, event.summary)
    ipcRenderer.on('pi:models', listener)

    return () => ipcRenderer.removeListener('pi:models', listener)
  },
  onDelta(callback) {
    const listener = (_event, event) => callback(event.threadId, event.text)
    ipcRenderer.on('pi:delta', listener)

    return () => ipcRenderer.removeListener('pi:delta', listener)
  },
  onDone(callback) {
    const listener = (_event, event) => callback(event.threadId)
    ipcRenderer.on('pi:done', listener)

    return () => ipcRenderer.removeListener('pi:done', listener)
  },
  onError(callback) {
    const listener = (_event, event) => callback(event.threadId, event.message)
    ipcRenderer.on('pi:error', listener)

    return () => ipcRenderer.removeListener('pi:error', listener)
  },
  onApprovalRequired(callback) {
    const listener = (_event, event) => callback(event.id, event.toolName, event.summary)
    ipcRenderer.on('approval:required', listener)

    return () => ipcRenderer.removeListener('approval:required', listener)
  },
  onToolEvent(callback) {
    const listener = (_event, event) => callback(event.threadId, event.kind, event.text)
    ipcRenderer.on('pi:tool-event', listener)

    return () => ipcRenderer.removeListener('pi:tool-event', listener)
  },
  onContextStats(callback) {
    const listener = (_event, event) => callback(
      event.threadId,
      event.modelName,
      event.inputTokens,
      event.outputTokens,
      event.contextUsed,
      event.contextMax,
      event.contextPercent
    )
    ipcRenderer.on('pi:context-stats', listener)

    return () => ipcRenderer.removeListener('pi:context-stats', listener)
  },
  onChromeStatus(callback) {
    const listener = (_event, event) => callback(event.connected, event.summary)
    ipcRenderer.on('chrome:status', listener)

    return () => ipcRenderer.removeListener('chrome:status', listener)
  },
  onFolderChosen(callback) {
    const listener = (_event, event) => callback(event.path)
    ipcRenderer.on('folder:chosen', listener)

    return () => ipcRenderer.removeListener('folder:chosen', listener)
  },
  onFilesChosen(callback) {
    const listener = (_event, event) => callback((event.paths || []).join('\n'))
    ipcRenderer.on('files:chosen', listener)

    return () => ipcRenderer.removeListener('files:chosen', listener)
  },
  onMenuAction(callback) {
    const listener = (_event, event) => callback(event.action || '')
    ipcRenderer.on('menu:action', listener)

    return () => ipcRenderer.removeListener('menu:action', listener)
  },
  onAbout(callback) {
    const listener = (_event, event) => callback(
      event.appVersion || '',
      event.piVersion || '',
      event.electronVersion || '',
      event.nodeVersion || '',
      event.platform || ''
    )
    ipcRenderer.on('app:about', listener)

    return () => ipcRenderer.removeListener('app:about', listener)
  }
})
