// WebSocket Protocol Types - Aligned with Rust backend

// ============================================================================
// Inbound Messages (Client -> Server)
// ============================================================================

export interface WsInboundMessage {
  type: 'message'
  content: string
  attachments?: Attachment[]
  channel?: string
  chat_id?: string
  sender_id?: string
}

export interface WsInboundCommand {
  type: 'command'
  payload: string
}

export interface WsInboundPing {
  type: 'ping'
}

export type WsInbound = WsInboundMessage | WsInboundCommand | WsInboundPing

// ============================================================================
// Outbound Messages (Server -> Client)
// ============================================================================

export interface Attachment {
  path: string
  media_type: string
  mime_type?: string
  content_base64?: string
  file_name?: string
}

export interface AssistantResponse {
  type: 'assistant_response'
  id: string
  content: string
  role: string
  attachments?: Attachment[]
  subagent_task_id?: string
  topic_id?: string
  timestamp?: number
  reasoning_content?: string
}

export interface ToolCall {
  type: 'tool_call'
  id: string
  tool_call_id: string
  tool_name: string
  arguments: unknown
  content: string
  role: string
  subagent_task_id?: string
  topic_id?: string
  timestamp?: number
  reasoning_content?: string
}

export interface ToolResult {
  type: 'tool_result'
  id: string
  tool_call_id: string
  tool_name: string
  content: string
  role: string
  subagent_task_id?: string
  topic_id?: string
  duration_ms?: number
  timestamp?: number
}

export interface ToolPending {
  type: 'tool_pending'
  id: string
  tool_call_id: string
  tool_name: string
  content: string
  role: string
  resume_hint: string
  subagent_task_id?: string
  topic_id?: string
  timestamp?: number
}

export interface WsError {
  type: 'error'
  code: string
  message: string
  timestamp?: number
}

export interface TaskStarted {
  type: 'task_started'
  task_id: string
  description: string
  subagent_type: string
  topic_id?: string
}

export interface SessionEstablished {
  type: 'session_established'
  session_id: string
}

export interface SessionCreated {
  type: 'session_created'
  session_id: string
  title: string
}

export interface SessionSummary {
  session_id: string
  title: string
  channel_name: string
  chat_id: string
  message_count: number
  last_active_at: number
  archived_at?: number
}

export interface SessionList {
  type: 'session_list'
  sessions: SessionSummary[]
  current_session_id?: string
  channel_name?: string  // 新增：标识所属通道
}

export interface SessionLoaded {
  type: 'session_loaded'
  session_id: string
  title: string
  message_count: number
}

export interface SessionSaved {
  type: 'session_saved'
  session_id: string
  filepath: string
}

export interface TopicSummary {
  topic_id: string
  session_id: string
  title: string
  description?: string
  message_count: number
  created_at: number
  last_active_at: number
}

export interface TopicList {
  type: 'topic_list'
  topics: TopicSummary[]
  session_id: string
}

export interface Channel {
  id: string
  name: string
  description?: string
  isWritable: boolean
}

export interface ChannelList {
  type: 'channel_list'
  channels: Channel[]
}

export interface Pong {
  type: 'pong'
}

export interface MemorySummary {
  id: string
  namespace: string
  memory_key: string
  content: string
  created_at: number
  updated_at: number
}

export interface MemoryList {
  type: 'memory_list'
  memories: MemorySummary[]
}

export interface SkillSummary {
  name: string
  description: string
  source: string
}

export interface SkillList {
  type: 'skill_list'
  skills: SkillSummary[]
}

export interface TodoItemSummary {
  id: string
  content: string
  status: string
  priority: string
  created_at: number
  updated_at: number
}

export interface TodoList {
  type: 'todo_list'
  todos: TodoItemSummary[]
  scope_key: string
}

export interface SchedulerJobSessionLookup {
  channel: string
  chat_id: string
}

export interface SchedulerJobSummary {
  id: string
  kind: string
  schedule: unknown
  enabled: boolean
  state: string
  last_status?: string
  last_error?: string
  run_count: number
  max_runs?: number
  last_fired_at?: number
  next_fire_at?: number
  created_at: number
  session_lookup?: SchedulerJobSessionLookup
}

export interface SchedulerJobList {
  type: 'scheduler_job_list'
  jobs: SchedulerJobSummary[]
}

export interface TaskMessagesLoaded {
  type: 'task_messages_loaded'
  task_id: string
  description: string
  subagent_type: string
  status: string
  summary?: string
}

export interface ExecutionCancelled {
  type: 'execution_cancelled'
  message: string
}

export type WsOutbound =
  | AssistantResponse
  | ToolCall
  | ToolResult
  | ToolPending
  | WsError
  | TaskStarted
  | SessionEstablished
  | SessionCreated
  | SessionList
  | SessionLoaded
  | SessionSaved
  | TopicList
  | ChannelList
  | TaskMessagesLoaded
  | SchedulerJobList
  | MemoryList
  | SkillList
  | TodoList
  | ExecutionCancelled
  | Pong

// ============================================================================
// Commands
// ============================================================================

export interface CreateSessionCommand {
  type: 'create_session'
  title?: string
}

export interface ListSessionsCommand {
  type: 'list_sessions'
  include_archived: boolean
}

export interface SwitchTopicCommand {
  type: 'switch_topic'
  topic_id: string
}

export interface SaveTopicCommand {
  type: 'save_topic'
  filepath?: string
  include_subagents: boolean
}

export interface SaveSessionCommand {
  type: 'save_session'
  filepath?: string
  include_all: boolean
  include_subagents: boolean
}

export interface LoadTopicCommand {
  type: 'load_topic'
  topic_id: string
}

export interface GetCurrentSessionCommand {
  type: 'get_current_session'
}

export interface HelpCommand {
  type: 'help'
}

export interface ListChannelsCommand {
  type: 'list_channels'
}

export interface ListSessionsByChannelCommand {
  type: 'list_sessions_by_channel'
  channel_name: string
  include_archived: boolean
}

export interface ListTopicsCommand {
  type: 'list_topics'
  session_id: string
}

export interface LoadTaskMessagesCommand {
  type: 'load_task_messages'
  task_id: string
}

export interface ListSchedulerJobsCommand {
  type: 'list_scheduler_jobs'
}

export interface LoadChatMessagesCommand {
  type: 'load_chat_messages'
  channel: string
  chat_id: string
}

export interface DeleteTopicCommand {
  type: 'delete_topic'
  topic_id: string
}

export interface StopExecutionCommand {
  type: 'stop_execution'
}

export interface ListMemoriesCommand {
  type: 'list_memories'
}

export interface CreateMemoryCommand {
  type: 'create_memory'
  namespace: string
  key: string
  content: string
}

export interface UpdateMemoryCommand {
  type: 'update_memory'
  id: string
  content: string
}

export interface DeleteMemoryCommand {
  type: 'delete_memory'
  id: string
}

export interface ListSkillsCommand {
  type: 'list_skills'
}

export interface ListTodosCommand {
  type: 'list_todos'
  task_id?: string
}

export type Command =
  | CreateSessionCommand
  | ListSessionsCommand
  | SwitchTopicCommand
  | SaveTopicCommand
  | SaveSessionCommand
  | LoadTopicCommand
  | GetCurrentSessionCommand
  | HelpCommand
  | ListChannelsCommand
  | ListSessionsByChannelCommand
  | ListTopicsCommand
  | LoadTaskMessagesCommand
  | ListSchedulerJobsCommand
  | LoadChatMessagesCommand
  | DeleteTopicCommand
  | StopExecutionCommand
  | ListMemoriesCommand
  | CreateMemoryCommand
  | UpdateMemoryCommand
  | DeleteMemoryCommand
  | ListSkillsCommand
  | ListTodosCommand

// ============================================================================
// UI Types
// ============================================================================

export interface ChatMessage {
  id: string
  role: 'user' | 'assistant' | 'tool'
  content: string
  timestamp: number
  type?: 'message' | 'tool_call' | 'tool_result' | 'tool_pending' | 'merged_tool'
  toolName?: string
  toolCallId?: string
  arguments?: unknown
  attachments?: Attachment[]
  status?: 'calling' | 'result' | 'pending'
  resultContent?: string
  callContent?: string
  subagentTaskId?: string
  durationMs?: number
  reasoningContent?: string
}

/** task 工具返回的 JSON 结构 */
export interface TaskToolResult {
  status: 'success' | 'failed' | 'timeout'
  summary: string
  output: string
  task_id: string
}

export interface Topic {
  id: string
  session_id: string
  title: string
  description?: string
  message_count: number
  created_at: number
  updated_at: number
}

export interface Session {
  id: string
  channel_name: string
  title?: string
  created_at: number
  updated_at: number
}

export type ConnectionStatus = 'connecting' | 'connected' | 'disconnected' | 'error'
