// 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
}

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

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

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

// ---- 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
}

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

export interface MessageContent {
  text?: 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
}
