import { contextBridge, ipcRenderer } from 'electron'

contextBridge.exposeInMainWorld('electronAPI', {
  window: {
    dragMove: (deltaX: number, deltaY: number) => ipcRenderer.invoke('window:dragMove', { deltaX, deltaY }),
    dragEnd: (x: number, y: number) => ipcRenderer.invoke('window:dragEnd', { x, y }),
    resizeMain: (width: number) => ipcRenderer.invoke('window:resizeMain', width)
  },

  screenshot: {
    start: () => ipcRenderer.invoke('screenshot:start'),
    capture: (region: { x: number; y: number; width: number; height: number }) =>
      ipcRenderer.invoke('screenshot:capture', region),
    cancel: () => ipcRenderer.invoke('screenshot:cancel'),
    getLastImage: () => ipcRenderer.invoke('screenshot:getLastImage'),
    onCompleted: (callback: (base64: string) => void) => {
      const handler = (_event: any, base64: string) => callback(base64)
      ipcRenderer.on('screenshot:completed', handler)
      return () => ipcRenderer.removeListener('screenshot:completed', handler)
    }
  },

  ai: {
    grade: (data: { imageBase64: string; rubric: string; apiConfig: { apiKey: string; baseUrl: string; model: string } }) =>
      ipcRenderer.invoke('ai:grade', data)
  },

  settings: {
    get: () => ipcRenderer.invoke('settings:get'),
    set: (settings: Record<string, unknown>) => ipcRenderer.invoke('settings:set', settings),
    selectDirectory: () => ipcRenderer.invoke('settings:selectDirectory')
  },

  history: {
    list: () => ipcRenderer.invoke('history:list'),
    delete: (id: number) => ipcRenderer.invoke('history:delete', id),
    getById: (id: number) => ipcRenderer.invoke('history:getById', id)
  },

  grading: {
    submit: (data: {
      questionId: number
      imagePath: string
      aiScore: number
      teacherScore: number
      confidence: number
      studentAnswer?: string
    }) => ipcRenderer.invoke('grading:submit', data),
    storeData: (data: any) => ipcRenderer.invoke('grading:storeData', data),
    getData: () => ipcRenderer.invoke('grading:getData')
  },

  rubric: {
    storeData: (data: any) => ipcRenderer.invoke('rubric:storeData', data),
    getData: () => ipcRenderer.invoke('rubric:getData'),
    exportJson: () => ipcRenderer.invoke('rubric:exportJson'),
    importJson: () => ipcRenderer.invoke('rubric:importJson'),
    getNextIndex: () => ipcRenderer.invoke('rubric:getNextIndex'),
    setNextIndex: (idx: number) => ipcRenderer.invoke('rubric:setNextIndex', idx),
    resetNextIndex: () => ipcRenderer.invoke('rubric:resetNextIndex'),
    onUpdated: (callback: () => void) => {
      const handler = () => callback()
      ipcRenderer.on('rubric:updated', handler)
      return () => ipcRenderer.removeListener('rubric:updated', handler)
    }
  },

  template: {
    list: () => ipcRenderer.invoke('template:list'),
    save: (data: { name: string; course: string; questions?: any[] }) => ipcRenderer.invoke('template:save', data),
    delete: (id: number) => ipcRenderer.invoke('template:delete', id),
    apply: (id: number) => ipcRenderer.invoke('template:apply', id),
    exportJson: () => ipcRenderer.invoke('template:exportJson'),
    importJson: () => ipcRenderer.invoke('template:importJson')
  },

  dialog: {
    open: (type: string, data?: any) => ipcRenderer.invoke('dialog:open', { type, data }),
    close: () => ipcRenderer.send('dialog:close'),
    getPayload: (type: string) => ipcRenderer.invoke('dialog:getPayload', type),
    openImage: () => ipcRenderer.invoke('dialog:openImage')
  }
})
