/**
 * RemoteLink Agent - Preload Script
 * Exposes safe APIs to the renderer process
 */

import { contextBridge, ipcRenderer } from 'electron'

// Define types for exposed API
interface AgentState {
  isConnected: boolean
  currentSessionCode: string | null
  sessionCodeExpiry: string | null
}

interface ScreenSource {
  id: string
  name: string
  thumbnail: string
}

interface Monitor {
  id: number
  index: number
  label: string
  width: number
  height: number
  primary: boolean
}

interface SessionCodeEvent {
  code: string
  expiresAt: string
  expiresIn: number
}

// Expose APIs to renderer
contextBridge.exposeInMainWorld('remotelink', {
  // Get current agent state
  getAgentState: (): Promise<AgentState> => {
    return ipcRenderer.invoke('get-agent-state')
  },

  // Set access key for registration
  setAccessKey: (accessKey: string): Promise<void> => {
    return ipcRenderer.invoke('set-access-key', accessKey)
  },

  // Generate a new session code
  generateSessionCode: (): Promise<string | null> => {
    return ipcRenderer.invoke('generate-session-code')
  },

  // Get available screen sources for sharing
  getScreenSources: (): Promise<ScreenSource[]> => {
    return ipcRenderer.invoke('get-screen-sources')
  },

  // Get connected monitors
  getMonitors: (): Promise<Monitor[]> => {
    return ipcRenderer.invoke('get-monitors')
  },

  // Event listeners
  onSessionCodeGenerated: (callback: (event: SessionCodeEvent) => void) => {
    ipcRenderer.on('session-code-generated', (_event, data) => callback(data))
    return () => {
      ipcRenderer.removeAllListeners('session-code-generated')
    }
  },

  onIncomingConnection: (callback: (data: { sessionCodeId: string; usedBy: string }) => void) => {
    ipcRenderer.on('incoming-connection', (_event, data) => callback(data))
    return () => {
      ipcRenderer.removeAllListeners('incoming-connection')
    }
  },

  onRegistered: (callback: (data: { success: boolean; machineId: string }) => void) => {
    ipcRenderer.on('registered', (_event, data) => callback(data))
    return () => {
      ipcRenderer.removeAllListeners('registered')
    }
  },

  onNavigate: (callback: (page: string) => void) => {
    ipcRenderer.on('navigate', (_event, page) => callback(page))
    return () => {
      ipcRenderer.removeAllListeners('navigate')
    }
  },
})

// Type declaration for window object
declare global {
  interface Window {
    remotelink: {
      getAgentState: () => Promise<AgentState>
      setAccessKey: (accessKey: string) => Promise<void>
      generateSessionCode: () => Promise<string | null>
      getScreenSources: () => Promise<ScreenSource[]>
      getMonitors: () => Promise<Monitor[]>
      onSessionCodeGenerated: (callback: (event: SessionCodeEvent) => void) => () => void
      onIncomingConnection: (callback: (data: { sessionCodeId: string; usedBy: string }) => void) => () => void
      onRegistered: (callback: (data: { success: boolean; machineId: string }) => void) => () => void
      onNavigate: (callback: (page: string) => void) => () => void
    }
  }
}
