/**
 * desktop/src/tray.ts — the menubar/system-tray icon that keeps the app alive
 * after its window is hidden (the embedded server must keep running so remote
 * devices stay connected — the vibe-coding scenario).
 *
 * Icon requirement: `iconPath` MUST point at an existing image. new Tray() with a
 * missing/invalid path can throw on some platforms; that failure surfaces to the
 * caller rather than being swallowed (a broken install should be visible, not
 * silently degrade). The caller owns whether to guard startup around it.
 */
import { Menu, Tray } from 'electron'

const TRAY_TOOLTIP = 'Web Terminal'

export interface TrayHandlers {
  show(): void
  quit(): void
}

export function createTray(iconPath: string, handlers: TrayHandlers): Tray {
  const tray = new Tray(iconPath)

  const menu = Menu.buildFromTemplate([
    { label: 'Show Web Terminal', click: handlers.show },
    { type: 'separator' },
    { label: 'Quit', click: handlers.quit },
  ])

  tray.setToolTip(TRAY_TOOLTIP)
  tray.setContextMenu(menu)
  return tray
}
