// Shared TypeScript types for the Nanobot UI

export type AgentState = "idle" | "listening" | "thinking" | "speaking";
export type CardLane = "attention" | "work" | "context" | "history";
export type CardState = "active" | "stale" | "resolved" | "superseded" | "archived";
export type JsonValue =
  | string
  | number
  | boolean
  | null
  | { [key: string]: JsonValue }
  | JsonValue[];

export interface CardSelectionRange {
  kind: "git_diff_range";
  file_path: string;
  file_label: string;
  range_label: string;
  label: string;
  old_start: number | null;
  old_end: number | null;
  new_start: number | null;
  new_end: number | null;
}

export interface CardMessageMetadata {
  card_id?: string;
  card_slot?: string;
  card_title?: string;
  card_lane?: CardLane;
  card_template_key?: string;
  card_context_summary?: string;
  card_response_value?: string;
  card_selection_label?: string;
  card_selection?: JsonValue;
  card_live_content?: JsonValue;
  context_label?: string;
}

export interface SessionSummary {
  chat_id: string;
  key: string;
  title: string;
  preview: string;
  created_at: string;
  updated_at: string;
  message_count: number;
}

export interface SessionHistoryLine {
  role: string;
  text: string;
  timestamp: string;
}

export type ServerMessage =
  | { type: "agent_state"; state: AgentState }
  | {
      type: "message";
      role: string;
      content: string;
      is_progress: boolean;
      is_tool_hint: boolean;
      timestamp: string;
      context_label?: string;
    }
  | {
      type: "card";
      id: string;
      kind: "text" | "question";
      title: string;
      content: string;
      question: string;
      choices: string[];
      response_value: string;
      slot: string;
      lane: CardLane;
      priority: number;
      state: CardState;
      template_key: string;
      template_state: Record<string, JsonValue>;
      context_summary: string;
      created_at: string;
      updated_at: string;
    }
  | {
      type: "workbench";
      id: string;
      chat_id: string;
      kind: "text" | "question";
      title: string;
      content: string;
      question: string;
      choices: string[];
      response_value: string;
      slot: string;
      template_key: string;
      template_state: Record<string, JsonValue>;
      context_summary: string;
      promotable: boolean;
      source_card_id: string;
      created_at: string;
      updated_at: string;
    }
  | { type: "error"; error: string }
  | { type: "pong" };

export type ClientMessage =
  | { type: "voice-ptt"; pressed: boolean; metadata?: CardMessageMetadata; chat_id?: string }
  | { type: "command"; command: string; chat_id?: string }
  | { type: "card-response"; card_id: string; value: string }
  | { type: "ping" };

export interface LogLine {
  id: number;
  role: string;
  text: string;
  timestamp: string;
  contextLabel?: string;
}

export interface CardItem {
  id: number;
  serverId?: string;
  kind: "text" | "question";
  content: string;
  title: string;
  question?: string;
  choices?: string[];
  responseValue?: string;
  slot?: string;
  lane: CardLane;
  priority: number;
  state: CardState;
  templateKey?: string;
  templateState?: Record<string, JsonValue>;
  contextSummary?: string;
  createdAt: string;
  updatedAt: string;
}

export interface WorkbenchItem {
  id: string;
  chatId: string;
  kind: "text" | "question";
  title: string;
  content: string;
  question?: string;
  choices?: string[];
  responseValue?: string;
  slot?: string;
  templateKey?: string;
  templateState?: Record<string, JsonValue>;
  contextSummary?: string;
  promotable: boolean;
  sourceCardId?: string;
  createdAt: string;
  updatedAt: string;
}
