import { contextBridge, ipcRenderer } from 'electron'

const api = {
  platform: process.platform,
  gitea: {
    /** Whether the main process has a gitea token + target repo configured. */
    status: () => ipcRenderer.invoke('gitea:status'),
    /** The last persisted snapshot, for instant boot before the fresh reconcile. */
    boot: () => ipcRenderer.invoke('gitea:boot'),
    /** Full read of the managed repo — every issue + milestone. */
    reconcile: () => ipcRenderer.invoke('gitea:reconcile'),
    /** One issue by index, normalized (or null if unconfigured). */
    getIssue: (index: number) => ipcRenderer.invoke('gitea:getIssue', index),
    /** Apply an estimate/priority change (the write path); resolves to the plan + fresh issue. */
    applyChange: (change: unknown) => ipcRenderer.invoke('gitea:applyChange', change),
    /** File a set of captured issues with their est/* + p/* labels. */
    createIssues: (issues: unknown) => ipcRenderer.invoke('gitea:createIssues', issues),
  },
  pmstate: {
    /** Read the directive ledger from the pm-state repo. */
    directives: () => ipcRenderer.invoke('pmstate:directives'),
  },
  model: {
    /** Whether a model endpoint is configured (else the UI keeps the scripted Reginald). */
    status: () => ipcRenderer.invoke('model:status'),
    /** One agent turn: messages in, Reginald's prose + the tools it consulted out. */
    chat: (messages: unknown) => ipcRenderer.invoke('model:chat', messages),
    /** Decompose a braindump into a proposed issue set (capture_work). */
    capture: (braindump: string) => ipcRenderer.invoke('model:capture', braindump),
  },
}

export type CommiteaApi = typeof api

contextBridge.exposeInMainWorld('commitea', api)
