/**
 * Message Segment Types & Chat DTOs
 */

import type { UserDTO } from './user';
import type { GroupResponse } from './group';

export type ConversationType = 'private' | 'group';
export type MessageContentType = 'text' | 'image' | 'video' | 'audio' | 'file';
export type MessageStatus = 'normal' | 'recalled' | 'deleted';

// ==================== Segment Types ====================

export type SegmentType = 'text' | 'image' | 'voice' | 'video' | 'file' | 'at' | 'reply' | 'face' | 'link' | 'vote' | 'post_ref';

export interface TextSegmentData {
  content?: string;
  text?: string;
}

export interface ImageSegmentData {
  url: string;
  width?: number;
  height?: number;
  thumbnail_url?: string;
  file_size?: number;
}

export interface VoiceSegmentData {
  url: string;
  duration?: number;
  file_size?: number;
}

export interface VideoSegmentData {
  url: string;
  width?: number;
  height?: number;
  duration?: number;
  thumbnail_url?: string;
  file_size?: number;
}

export interface FileSegmentData {
  url: string;
  name: string;
  size?: number;
  mime_type?: string;
}

export interface AtSegmentData {
  user_id: string;
  nickname?: string;
}

export interface ReplySegmentData {
  id: string;
  seq?: number;
}

export interface FaceSegmentData {
  id: number;
  name?: string;
  url?: string;
}

export interface LinkSegmentData {
  url: string;
  title?: string;
  description?: string;
  image?: string;
}

export interface VoteOptionData {
  id: string;
  content: string;
}

export interface VoteSegmentData {
  options: VoteOptionData[];
  max_choices?: number;
  is_multi_choice?: boolean;
}

export interface PostRefSegmentData {
  post_id: string;
  title?: string;
  author?: {
    id: string;
    nickname: string;
    avatar?: string;
  };
  status?: string;
  accessible?: boolean;
  click_count?: number;
}

export type MessageSegment =
  | { type: 'text'; data: TextSegmentData }
  | { type: 'image'; data: ImageSegmentData }
  | { type: 'voice'; data: VoiceSegmentData }
  | { type: 'video'; data: VideoSegmentData }
  | { type: 'file'; data: FileSegmentData }
  | { type: 'at'; data: AtSegmentData }
  | { type: 'reply'; data: ReplySegmentData }
  | { type: 'face'; data: FaceSegmentData }
  | { type: 'link'; data: LinkSegmentData }
  | { type: 'vote'; data: VoteSegmentData }
  | { type: 'post_ref'; data: PostRefSegmentData };

// ==================== Chat DTOs ====================

export interface MessageResponse {
  id: string;
  conversation_id: string;
  sender_id: string;
  seq: number;
  segments: MessageSegment[];
  reply_to_id?: string;
  status: MessageStatus;
  category?: string;
  created_at: string;
  sender?: UserDTO;
  is_system_notice?: boolean;
  notice_content?: string;
}

export interface ConversationResponse {
  id: string;
  type: ConversationType;
  is_pinned?: boolean;
  notification_muted?: boolean;
  last_seq: number;
  last_message?: MessageResponse;
  last_message_at: string;
  unread_count: number;
  participants?: UserDTO[];
  member_count?: number;
  group?: GroupResponse;
  my_last_read_seq?: number;
  other_last_read_seq?: number;
  created_at: string;
  updated_at: string;
}

export interface ConversationParticipantResponse {
  user_id: string;
  last_read_seq: number;
  notification_muted: boolean;
  is_pinned: boolean;
}

export interface ConversationDetailResponse {
  id: string;
  type: ConversationType;
  is_pinned?: boolean;
  notification_muted?: boolean;
  last_seq: number;
  last_message?: MessageResponse;
  last_message_at: string;
  unread_count: number;
  participants: UserDTO[];
  my_last_read_seq: number;
  other_last_read_seq: number;
  group?: GroupResponse;
  created_at: string;
  updated_at: string;
}

export interface CreateConversationRequest {
  user_id: string;
}

export interface SendMessageRequest {
  segments?: MessageSegment[];
  content?: string;
  reply_to_id?: string;
}

export interface MarkReadRequest {
  last_read_seq: number;
}

export interface UnreadCountResponse {
  total_unread_count: number;
}

export interface ConversationUnreadCountResponse {
  conversation_id: string;
  unread_count: number;
}

export interface ConversationListResponse {
  list: ConversationResponse[];
  total: number;
  page: number;
  page_size: number;
  total_pages: number;
}

export interface MessageListResponse {
  messages: MessageResponse[];
  total: number;
  page: number;
  page_size: number;
}

export interface MessageSyncResponse {
  messages: MessageResponse[];
  has_more: boolean;
}