// Discriminated unions for SSE event payloads — eliminates `any` in the
// chat and workspace stores by giving each event type a concrete data shape.

// ---- Chat SSE events (streamChat / /agent/chat) ----

export interface ChatSessionEvent {
  type: 'session'
  data: string
}

export interface ChatToolUseEvent {
  type: 'tool_use'
  data: { name: string; id: string; args: Record<string, unknown> }
}

export interface ChatToolResultEvent {
  type: 'tool_result'
  data: { id: string; result: unknown; error?: string }
}

export interface ChatTextDeltaEvent {
  type: 'text_delta'
  data: string
}

export interface ChatTextEvent {
  type: 'text'
  data: string
  is_thinking?: boolean
}

export interface ChatDoneEvent {
  type: 'done'
  data?: { session_id?: string }
  session_id?: string
}

export interface ChatErrorEvent {
  type: 'error'
  data: string
}

// The turn was queued behind an in-flight turn for this session (plan
// 2026-08-03 F2). No live assistant stream follows in this response; the
// queued turn runs server-side when the gate frees and the poller surfaces it.
export interface ChatQueuedEvent {
  type: 'queued'
  data: string // session id
}

export type ChatEvent =
  | ChatSessionEvent
  | ChatToolUseEvent
  | ChatToolResultEvent
  | ChatTextDeltaEvent
  | ChatTextEvent
  | ChatDoneEvent
  | ChatErrorEvent
  | ChatQueuedEvent

// ---- Tool call result (merged from tool_use + tool_result SSE pairs) ----

export interface ToolCallResult {
  type: 'tool_use' | 'tool_result'
  name: string
  id?: string
  args?: Record<string, unknown>
  result?: unknown
  error?: string
  // Streaming command output for an in-flight `run` call — attached live from
  // the execution.output event stream (execstream.ts) while the call is still
  // running, so the tool card can show output as it arrives instead of all at
  // once when the tool_result lands. Not present on persisted/historical calls.
  liveOutput?: string
  // Plan step this call belongs to (current generation only). Attached by
  // ChatThread from the activity log so the inline trace can group a turn's
  // tool calls under their step. Undefined for orphan calls (no plan / older
  // generation / plan-less Q&A).
  stepSeq?: number
}

// ---- Message content (persisted messages from /agent/sessions/:id) ----

export interface MessageContent {
  text?: string
  thinking?: string
  tool_calls?: ToolCallResult[]
}

// ---- Live event stream (SSE /api/v1/events/stream) ----

export interface PlanProposedData {
  steps: Array<{ id: string; seq: number; title: string; detail?: string; target_slug?: string }>
  appended?: boolean
}

export interface PlanStepEventData {
  step_id?: string
  seq?: number
  status?: string
  execution_id?: string
}

export interface QuestionRaisedData {
  question_id: string
  prompt?: string
  why?: string
  options?: string[]
  entities?: string[]
}

export interface QuestionAnsweredData {
  question_id: string
  answer?: string
}

export interface EntityTouchedData {
  slug?: string
  tool?: string
}

export interface HealthChangedData {
  slug?: string
  from?: string
  to?: string
}

// ---- Wails desktop bridge (injected into window) ----

export interface WailsCall {
  ByName: (method: string, ...args: unknown[]) => unknown
}

export interface WailsGlobal {
  Call?: WailsCall
}
