// ============================================================
// CamTalk 前端类型定义
// 来源：docs/03-接口文档.md
// ============================================================

// ---- 会话模型 ----

export interface SessionConfig {
  ttsEnabled: boolean;
  detailLevel: "low" | "high";
  language: string;
  scenario: string;    // 情景 ID，如 "free_chat"、"interviewer"
  cameraDeviceId?: string;  // 摄像头设备 ID，空 = 系统默认
  micDeviceId?: string;     // 麦克风设备 ID，空 = 系统默认
}

export type Theme = "dark" | "light";

export interface Session {
  sessionId: string;
  createdAt: string;
  config: SessionConfig;
}

/** 会话摘要（侧边栏列表用） */
export interface SessionSummary {
  id: string;
  title: string;
  createdAt: number;
  lastActiveAt: number;
  messageCount: number;
  preview: string;
}

// ---- 聊天消息 ----

export interface ChatMessage {
  id: string;
  role: "user" | "assistant" | "system";
  content: string;
  imageUrl?: string;
  timestamp: number;
  tokensUsed?: number;
  latencyMs?: number;
  model?: string;
}

// ---- WebSocket 通用信封 ----

export interface WsMessage {
  type: string;
  request_id?: string;
  timestamp?: number;
  [key: string]: unknown;
}

// ---- 客户端 → 服务端消息 ----

export interface QueryMessage {
  type: "query";
  request_id: string;
  image: string;       // Base64 JPEG（不含 data: 前缀）
  audio: string;       // Base64 PCM 16kHz（文本输入时为空字符串）
  text?: string;       // 用户手动输入的文本（有值时跳过 STT）
  mime_type?: string;  // 默认 "audio/pcm"
}

export interface ConfigMessage {
  type: "config";
  payload: {
    tts_enabled?: boolean;
    detail_level?: "low" | "high";
    language?: string;
    scenario?: string;
  };
}

export interface InterruptMessage {
  type: "interrupt";
  request_id?: string;
}

export interface PingMessage {
  type: "ping";
}

export type ClientMessage =
  | QueryMessage
  | ConfigMessage
  | InterruptMessage
  | PingMessage;

// ---- 服务端 → 客户端消息 ----

export interface ConnectedMessage {
  type: "connected";
  session_id: string;
  server_version: string;
}

export interface STTResultMessage {
  type: "stt_result";
  request_id: string;
  text: string;
  is_final: boolean;
}

export interface LLMChunkMessage {
  type: "llm_chunk";
  request_id: string;
  delta: string;
  role: "assistant";
}

export interface LLMDoneMessage {
  type: "llm_done";
  request_id: string;
  full_text: string;
  tokens_used: {
    prompt: number;
    completion: number;
    total: number;
  };
  model: string;
  latency_ms: number;
}

export interface TTSAudioMessage {
  type: "tts_audio";
  request_id: string;
  audio: string;       // Base64 音频片段
  mime_type: string;   // "audio/mp3" 或 "audio/pcm"
  is_last: boolean;    // 当前句子的音频是否完整（每句结束时为 true）
  final: boolean;      // 整轮 TTS 是否结束
}

export interface ErrorMessage {
  type: "error";
  request_id?: string;
  code: string;
  message: string;
}

export interface PongMessage {
  type: "pong";
}

export type ServerMessage =
  | ConnectedMessage
  | STTResultMessage
  | LLMChunkMessage
  | LLMDoneMessage
  | TTSAudioMessage
  | ErrorMessage
  | PongMessage;

// ---- 错误码 ----

export type ErrorCode =
  | "INVALID_MESSAGE"
  | "SESSION_NOT_FOUND"
  | "RATE_LIMITED"
  | "IMAGE_TOO_LARGE"
  | "AUDIO_TOO_SHORT"
  | "LLM_TIMEOUT"
  | "LLM_ERROR"
  | "STT_ERROR"
  | "TTS_ERROR"
  | "INTERNAL_ERROR";
