import { contextBridge, ipcRenderer } from 'electron';

contextBridge.exposeInMainWorld('electronAPI', {
  isElectron: true,

  // Sauvegarde persistante (fichier JSON dans userData)
  saveData:          (json: string) => ipcRenderer.send('save-data', json),
  loadData:          () => ipcRenderer.invoke('load-data'),

  // Alarme
  triggerAlarm:      (enclosName: string) => ipcRenderer.send('trigger-alarm', { enclosName }),
  showNotification:  (title: string, body: string) => ipcRenderer.send('show-notification', { title, body }),
  sendNtfy:          (url: string, title: string, message: string) => ipcRenderer.send('send-ntfy', { url, title, message }),
  focusWindow:       () => ipcRenderer.send('focus-window'),
  showConfirm:       (title: string, message: string, detail: string) => ipcRenderer.invoke('show-confirm', { title, message, detail }),
  onPlayAlarmSound:  (cb: () => void) => ipcRenderer.on('play-alarm-sound', () => cb()),

  // Export / Import fichier
  exportFile:        (data: string, defaultName: string) => ipcRenderer.invoke('export-file', { data, defaultName }),
  importFile:        () => ipcRenderer.invoke('import-file'),

  // Version
  getVersion:        () => ipcRenderer.invoke('get-version'),
  onAppVersion:      (cb: (v: string) => void) => ipcRenderer.on('app-version', (_e, v) => cb(v)),

  // Mises à jour
  installUpdate:         () => ipcRenderer.send('install-update'),
  onUpdateAvailable:     (cb: (info: any) => void) => ipcRenderer.on('update-available',   (_e, info) => cb(info)),
  onUpdateNotAvailable:  (cb: () => void) => ipcRenderer.on('update-not-available', () => cb()),
  onUpdateDownloading:   (cb: (info: any) => void) => ipcRenderer.on('update-downloading', (_e, info) => cb(info)),
  onUpdateProgress:      (cb: (info: any) => void) => ipcRenderer.on('update-progress',    (_e, info) => cb(info)),
  onUpdateReady:         (cb: () => void) => ipcRenderer.on('update-ready',       () => cb()),
  onUpdateError:         (cb: (info: any) => void) => ipcRenderer.on('update-error',       (_e, info) => cb(info)),

  // Dev mode badge
  onDevMode:             (cb: () => void) => ipcRenderer.on('dev-mode', () => cb()),
});
