// 聊天相关类型定义

/** 消息角色 */
export type MessageRole = 'user' | 'assistant' | 'system' | 'action';

/** 消息显示类型 (区分聊天消息与动作消息) */
export type MessageDisplayType = 'chat' | 'action' | 'system' | 'thinking' | 'tool_progress' | 'system_info' | 'markdown' | 'code';

/** 对话模式 */
export type ChatMode = 'text' | 'voice_msg' | 'voice_assistant';

/** 后台思考状态 */
export type BackgroundThinkingStatus = 'idle' | 'thinking' | 'done';

/** 语音片段 */
export interface VoiceSegment {
  index: number;
  text: string;
  audioUrl?: string;
  durationMs?: number;
}

/** 消息附件 (图片等) */
export interface MessageAttachment {
  type: 'image';
  url: string;          // 图片 URL 或 data URL
  thumbnail_url?: string;
  filename?: string;
  width?: number;
  height?: number;
  size?: number;        // 文件大小 bytes
  description?: string; // AI 对图片的描述
}

/** 审查消息 (后端审查子会话输出的带类型消息) */
export interface ReviewMessage {
  type: 'action' | 'chat' | 'markdown' | 'code' | 'search_result';
  content: string;
  metadata?: Record<string, unknown>;
}

/** 客户端信息 (多端区分) */
export interface ClientInfo {
  client_id: string;
  device_name?: string;
  user_agent?: string;
}

/** 单条消息 */
export interface Message {
  id: string;
  role: MessageRole;
  content: string;
  audioUrl?: string;
  segments?: VoiceSegment[];
  attachments?: MessageAttachment[];
  timestamp: number;
  isStreaming?: boolean;
  /** 消息显示类型: 区分聊天消息与动作消息 */
  msgType?: MessageDisplayType;
  /** 消息来源客户端信息 (多端区分) */
  client_info?: ClientInfo;
  /** 类型特定元数据 (如 code 语言、search_result URL 等) */
  metadata?: Record<string, unknown>;
}

/** IoT 设备类型定义 */
export type IoTDeviceType = 'light' | 'ac' | 'curtain' | 'sensor' | 'lock';

/** IoT 设备 */
export interface IoTDevice {
  id: string;
  name: string;
  type: IoTDeviceType;
  status: string;
  brightness?: number;
  color?: string;
  temperature?: number;
  mode?: string;
  position?: number;
  value?: number;
  unit?: string;
  battery?: number;
  last_updated: string;
}

/** IoT 设备更新消息 */
export interface IoTDeviceUpdate {
  devices: IoTDevice[];
  total: number;
}

/** 多段消息子项 (子会话架构 multi_message 类型) */
export interface MultiMessageItem {
  index: number;
  content: string;
  msg_type?: string; // chat | action | system_info (由后端分类)
}

/** 流式片段 (子会话架构 stream_segments 类型) */
export interface StreamSegment {
  index: number;
  text: string;
}

/** WebSocket 客户端消息 */
export interface WSClientMessage {
  type: 'message' | 'voice_input' | 'voice_stream_start' | 'voice_stream_chunk' | 'voice_stream_end' | 'ping' | 'history';
  session_id?: string;
  mode?: ChatMode;
  content?: string;
  audio_data?: string; // base64
  format?: string;     // 音频格式 (voice_stream_start): webm, wav, pcm, opus
  language?: string;   // 识别语言 (voice_stream_start): zh, en, ja, ko, auto
  sequence?: number;   // 音频分片序号 (voice_stream_chunk)
  attachments?: MessageAttachment[];
  timestamp: number;
  client_id?: string;
  device_name?: string;
  user_agent?: string;
  client_msg_id?: string; // 客户端消息ID (跨端去重)
}

/** 通知类型 */
export type NotificationType = 'info' | 'warning' | 'success' | 'thinking' | 'reminder';

/** WebSocket 通知数据 */
export interface NotificationData {
  id: string;
  type: NotificationType;
  title: string;
  body: string;
  timestamp: string;
  data?: Record<string, unknown>;
}

/** 站内通知 (store 中使用，扩展了已读状态) */
export interface AppNotification extends NotificationData {
  read: boolean;
  createdAt: number; // 客户端收到时间
}

/** WebSocket 服务端消息 */
export interface WSServerMessage {
  type: 'stream_start' | 'response' | 'segment' | 'audio' | 'error' | 'device_update' | 'pong' | 'history_response' | 'stream_chunk' | 'stream_end' | 'background_thinking' | 'notification' | 'multi_message' | 'stream_segments' | 'review' | 'thinking' | 'tool_progress' | 'system_info' | 'voice_interim' | 'voice_final';
  message_id?: string;
  text?: string;
  content?: string;
  role?: string;
  msg_type?: string;
  session_id?: string;
  segments?: VoiceSegment[];
  full_audio_url?: string;
  response_mode?: ChatMode;
  tool_calls?: ToolCall[];
  error?: string;
  messages?: Message[];
  multi_messages?: MultiMessageItem[];
  stream_segments?: StreamSegment[];
  review_messages?: ReviewMessage[];
  devices?: IoTDevice[];
  thinking_status?: BackgroundThinkingStatus;
  thinking_content?: string;
  notification?: NotificationData;
  tool_progress?: ToolProgressInfo;
  system_info?: SystemInfoPayload;
  metadata?: Record<string, unknown>;
  protocol_version?: number;
  timestamp: number;
  client_info?: ClientInfo;
}

/** 工具进度信息 */
export interface ToolProgressInfo {
  tool_name: string;
  status: string; // started, running, completed, failed
  progress: number;
  message: string;
}

/** 系统信息负载 */
export interface SystemInfoPayload {
  level: string; // info, warning, error
  message: string;
  action?: string;
}

/** 工具调用 */
export interface ToolCall {
  name: string;
  arguments: Record<string, unknown>;
  result?: unknown;
}

/** 聊天状态 */
export interface ChatState {
  messages: Message[];
  isTyping: boolean;
  currentMode: ChatMode;
  continuousMode: boolean;
  backgroundThinkingStatus: BackgroundThinkingStatus;
}
