import { contextBridge, ipcRenderer } from "electron";

// ─── Expose safe API to renderer ─────────────────
contextBridge.exposeInMainWorld("electronAPI", {
  // ── App Info ──
  getAppVersion: () => ipcRenderer.invoke("app:version"),
  getPlatform: () => ipcRenderer.invoke("app:platform"),
  getIsDev: () => ipcRenderer.invoke("app:isDev"),

  // ── Window Controls ──
  minimizeWindow: () => ipcRenderer.send("window:minimize"),
  maximizeWindow: () => ipcRenderer.send("window:maximize"),
  closeWindow: () => ipcRenderer.send("window:close"),
  isMaximized: () => ipcRenderer.invoke("window:isMaximized"),

  // ── File Dialogs ──
  openFile: (options?: Electron.OpenDialogOptions) =>
    ipcRenderer.invoke("dialog:openFile", options),
  saveFile: (options?: Electron.SaveDialogOptions) =>
    ipcRenderer.invoke("dialog:saveFile", options),

  // ── Notifications ──
  showNotification: (title: string, body: string) =>
    ipcRenderer.send("notification:show", { title, body }),

  // ── Shell ──
  openExternal: (url: string) => ipcRenderer.send("shell:openExternal", url),
  openPath: (path: string) => ipcRenderer.send("shell:openPath", path),

  // ── Store ──
  storeGet: (key: string) => ipcRenderer.invoke("store:get", key),
  storeSet: (key: string, value: unknown) =>
    ipcRenderer.invoke("store:set", key, value),
  storeDelete: (key: string) => ipcRenderer.invoke("store:delete", key),

  // ── Updates ──
  checkForUpdates: () => ipcRenderer.invoke("updater:check"),
  downloadUpdate: () => ipcRenderer.invoke("updater:download"),
  quitAndInstall: () => ipcRenderer.send("updater:quitAndInstall"),

  // ── Update Events ──
  onUpdateAvailable: (callback: (info: unknown) => void) => {
    ipcRenderer.on("updater:available", (_event, info) => callback(info));
  },
  onUpdateDownloaded: (callback: (info: unknown) => void) => {
    ipcRenderer.on("updater:downloaded", (_event, info) => callback(info));
  },
  onUpdateProgress: (callback: (progress: unknown) => void) => {
    ipcRenderer.on("updater:progress", (_event, progress) => callback(progress));
  },
  onUpdateError: (callback: (error: string) => void) => {
    ipcRenderer.on("updater:error", (_event, error) => callback(error));
  },

  // ── Remove listener ──
  removeAllListeners: (channel: string) => {
    ipcRenderer.removeAllListeners(channel);
  },
});

// ─── Type declarations for renderer ──────────────
export interface ElectronAPI {
  getAppVersion: () => Promise<string>;
  getPlatform: () => Promise<string>;
  getIsDev: () => Promise<boolean>;
  minimizeWindow: () => void;
  maximizeWindow: () => void;
  closeWindow: () => void;
  isMaximized: () => Promise<boolean>;
  openFile: (options?: Electron.OpenDialogOptions) => Promise<string[] | undefined>;
  saveFile: (options?: Electron.SaveDialogOptions) => Promise<string | undefined>;
  showNotification: (title: string, body: string) => void;
  openExternal: (url: string) => void;
  openPath: (path: string) => void;
  storeGet: (key: string) => Promise<unknown>;
  storeSet: (key: string, value: unknown) => Promise<void>;
  storeDelete: (key: string) => Promise<void>;
  checkForUpdates: () => Promise<void>;
  downloadUpdate: () => Promise<void>;
  quitAndInstall: () => void;
  onUpdateAvailable: (callback: (info: unknown) => void) => void;
  onUpdateDownloaded: (callback: (info: unknown) => void) => void;
  onUpdateProgress: (callback: (progress: unknown) => void) => void;
  onUpdateError: (callback: (error: string) => void) => void;
  removeAllListeners: (channel: string) => void;
}

declare global {
  interface Window {
    electronAPI: ElectronAPI;
  }
}
