export type ChatTimelineRole = 'assistant' | 'system' | 'user';
export type ChatTimelineItemStatus = 'done' | 'error' | 'pending' | 'streaming';
export type ChatTimelineThinkingStatus = 'end' | 'error' | 'thinking';
export type ChatTimelineToolMode = 'approval' | 'auto';
export type ChatTimelineToolStatus =
  | 'approving'
  | 'error'
  | 'pending_approval'
  | 'rejected'
  | 'running'
  | 'success';
export type ChatTimelineStatusStatus = 'done' | 'running';
export type ChatTimelineStatusTone = 'muted';

export interface ChatImageAttachment {
  error?: string;
  height?: number;
  imageRef?: string;
  localId?: string;
  mimeType?: string;
  name: string;
  previewUrl: string;
  size?: number;
  status?: 'error' | 'ready' | 'uploading';
  uploadId?: string;
  width?: number;
}

export type ChatImageLoader = (previewUrl: string) => Promise<string>;

export interface ChatTimelineToolApprovalPayload {
  requestId: string;
  resumeToken: string;
  toolName: string;
  toolDisplayName?: string;
  toolCallId?: string;
  toolType?: string;
  input?: unknown;
  expiresAt?: string;
  metadata?: unknown;
}

export interface ChatTimelineKnowledgeHit {
  chunkContent?: string;
  chunkId?: string;
  id?: string;
  documentId?: string;
  documentName?: string;
  faqCollection?: boolean;
  knowledgeId?: string;
  knowledgeName?: string;
  knowledgeType?: string;
  metadata?: Record<string, unknown>;
  source?: string;
  sourceFileName?: string;
  sourceUri?: string;
  score?: number | string;
  title?: string;
  content?: string;
  [key: string]: any;
}

export interface ChatTimelineItemBase {
  createdAt?: number;
  id: string;
}

export interface ChatTimelineMessageItem extends ChatTimelineItemBase {
  images?: ChatImageAttachment[];
  knowledgeItems?: ChatTimelineKnowledgeHit[];
  parts: ChatTimelineMessagePart[];
  regenerable?: boolean;
  role: ChatTimelineRole;
  roundId?: string;
  roundCompleted?: boolean;
  roundNo?: number;
  status?: ChatTimelineItemStatus;
  selectedVariantIndex?: number;
  switchable?: boolean;
  type: 'message';
  variantCount?: number;
  variantIndex?: number;
}

export interface ChatTimelineToolItem extends ChatTimelineItemBase {
  approval?: ChatTimelineToolApprovalPayload;
  input?: unknown;
  mode: ChatTimelineToolMode;
  output?: unknown;
  rejectReason?: string;
  status: ChatTimelineToolStatus;
  toolCallId?: string;
  toolName: string;
  type: 'tool';
}

/**
 * @deprecated 工具审批已聚合到 ChatTimelineToolItem，保留类型用于旧调用方过渡。
 */
export interface ChatTimelineToolApprovalItem extends ChatTimelineItemBase {
  payload: ChatTimelineToolApprovalPayload;
  type: 'tool_approval';
}

export interface ChatTimelineKnowledgeItem extends ChatTimelineItemBase {
  items: ChatTimelineKnowledgeHit[];
  type: 'knowledge';
}

export interface ChatTimelineStatusItem extends ChatTimelineItemBase {
  label: string;
  presentation?: 'inline' | 'separator';
  status: ChatTimelineStatusStatus;
  statusKey: string;
  tone?: ChatTimelineStatusTone;
  type: 'status';
}

export interface ChatTimelineErrorItem extends ChatTimelineItemBase {
  message: string;
  type: 'error';
}

export type ChatTimelineItem =
  | ChatTimelineErrorItem
  | ChatTimelineKnowledgeItem
  | ChatTimelineMessageItem
  | ChatTimelineStatusItem
  | ChatTimelineToolApprovalItem
  | ChatTimelineToolItem;

export type ChatTimelineMessagePart =
  | {
      content: string;
      id: string;
      type: 'text';
    }
  | {
      content: string;
      expanded?: boolean;
      id: string;
      status: ChatTimelineThinkingStatus;
      type: 'thinking';
    };
