/**
 * AIUI ↔ Archy postMessage Protocol
 *
 * AIUI (iframe) communicates with Archy (host) via structured messages.
 * Archy acts as a context broker — AIUI never directly accesses node data.
 */

/** Data categories that AIUI can request access to */
export type AIContextCategory =
  | 'apps'
  | 'system'
  | 'network'
  | 'wallet'
  | 'files'
  | 'media'
  | 'search'
  | 'ai-local'
  | 'notes'
  | 'bitcoin'

/** Actions AIUI can request Archy to perform */
export type AIActionType = 'install-app' | 'open-app' | 'navigate' | 'launch-app' | 'search-web' | 'read-file' | 'tail-logs'

// ─── AIUI → Archy (Requests) ───────────────────────────────────────────────

export interface AIUIContextRequest {
  type: 'context:request'
  id: string
  category: AIContextCategory
  query?: string
}

export interface AIUIActionRequest {
  type: 'action:request'
  id: string
  action: AIActionType
  params: Record<string, string>
}

export interface AIUIReadyMessage {
  type: 'ready'
}

export interface AIUIThemeRequest {
  type: 'theme:request'
}

export type AIUIRequest =
  | AIUIContextRequest
  | AIUIActionRequest
  | AIUIReadyMessage
  | AIUIThemeRequest

// ─── Archy → AIUI (Responses) ──────────────────────────────────────────────

export interface ArchyContextResponse {
  type: 'context:response'
  id: string
  data: unknown
  permitted: boolean
}

export interface ArchyActionResponse {
  type: 'action:response'
  id: string
  success: boolean
  error?: string
  data?: unknown
}

export interface ArchyThemeResponse {
  type: 'theme:response'
  theme: {
    accent: string
    mode: 'dark'
  }
}

export interface ArchyPermissionsUpdate {
  type: 'permissions:update'
  categories: AIContextCategory[]
}

export type ArchyResponse =
  | ArchyContextResponse
  | ArchyActionResponse
  | ArchyThemeResponse
  | ArchyPermissionsUpdate

// ─── All messages ───────────────────────────────────────────────────────────

export type AIUIMessage = AIUIRequest | ArchyResponse

/** Protocol version for compatibility checks */
export const AIUI_PROTOCOL_VERSION = '1.0.0'

/** Message origin prefix used for validation */
export const AIUI_MESSAGE_PREFIX = 'aiui:'
