export type IslanderPageMeta = {
  title?: string
  purpose?: string
  hints?: string[]
  suggestions?: string[]
}

export type IslanderChatTurn = {
  role: 'user' | 'assistant'
  content: string
}

export type IslanderAction =
  | { type: 'navigate'; path: string }
  | { type: 'click'; ref: string }
  | { type: 'fill'; ref?: string; label?: string; value: string }
  | { type: 'select'; ref: string; value: string }
  | { type: 'focus'; ref: string }
  | { type: 'scroll'; ref?: string; top?: number }
  | { type: 'highlight'; ref: string }
  | { type: 'wait'; ms?: number }
  | { type: 'generateOutreachReply'; scan_post_id: string; count?: number }
  | { type: 'applyDraft'; value: string; label?: string }
  | { type: 'publishOutreach'; scan_post_id: string; text: string; confirm?: boolean }
  | { type: 'markHandled'; scan_post_id: string; status?: 'handled' | 'skipped' | 'pending' }
  | { type: 'expandKnowledgeGraph'; seed_query?: string; supplemental?: boolean }
  | { type: 'toggleGraphNode'; node_id: string; selected: boolean }
  | { type: 'startScan' }
  | { type: 'startViralScan' }
  | { type: 'startCopyMissionAnalyze' }
  | { type: 'startCopyMissionScan' }
  | { type: 'generateCopyMatrix' }
  | { type: 'generateCopyDraft'; scan_post_id: string }
  | { type: 'publishCopyDraft'; draft_id: string; confirm?: boolean }
  | { type: 'navigateToPersona8D' }

export type IslanderActionResult = {
  action: IslanderAction
  ok: boolean
  detail: string
}

export type PageElementSnapshot = {
  ref: string
  kind: string
  label: string
  meta?: string
  inView: boolean
}

export type PageSnapshot = {
  elements: PageElementSnapshot[]
  refMap: Map<string, HTMLElement>
}

export type IslanderActionHandler = (
  action: IslanderAction,
  ctx: IslanderExecutorContext,
) => Promise<IslanderActionResult | null>

export type IslanderExecutorContext = {
  navigate: (path: string) => void
  snapshot: PageSnapshot
  waitMs: (ms: number) => Promise<void>
  highlight: (el: HTMLElement) => void
  resolveRef: (ref: string) => HTMLElement | null
}