import type { ConversationResponse, MessageResponse, UserDTO } from '../../types/dto';
import type { IConversationListPagedSource } from './sources';

export interface MessageManagerState {
  conversations: Map<string, ConversationResponse>;
  conversationList: ConversationResponse[];
  messagesMap: Map<string, MessageResponse[]>;
  totalUnreadCount: number;
  systemUnreadCount: number;
  isWSConnected: boolean;
  currentConversationId: string | null;
  isLoadingConversations: boolean;
  loadingMessagesSet: Set<string>;
  isInitialized: boolean;
  typingUsersMap: Map<string, string[]>;
  mutedStatusMap: Map<string, boolean>;
}

export interface ReadStateRecord {
  timestamp: number;
  version: number;
  lastReadSeq: number;
  clearTimer?: ReturnType<typeof setTimeout>;
}

export interface MessageManagerConversationListDeps {
  remoteConversationListSource?: IConversationListPagedSource;
  localConversationListSource?: IConversationListPagedSource;
  remoteListKind?: 'cursor' | 'offset';
  remoteListPageSize?: number;
}

export interface HandleNewMessageOptions {
  suppressUnreadIncrement?: boolean;
  suppressVibration?: boolean;
  suppressConversationUpdates?: boolean;
  suppressAutoMarkAsRead?: boolean;
}

export interface IMessageDeduplication {
  isMessageProcessed(messageId: string): boolean;
  markMessageAsProcessed(messageId: string): void;
  cleanupProcessedMessageIds(): void;
}

export interface IUserCacheService {
  getSenderInfo(userId: string): Promise<UserDTO | null>;
  enrichMessagesWithSenderInfo(
    conversationId: string,
    messages: MessageResponse[],
    notifyUpdate: (conversationId: string, messages: MessageResponse[]) => void
  ): void;
}

export interface IReadReceiptManager {
  markAsRead(conversationId: string, seq: number): Promise<void>;
  markAllAsRead(): Promise<void>;
  beginReadOperation(conversationId: string, seq: number): number;
  endReadOperation(conversationId: string, version: number, success: boolean): void;
  isReadOperationPending(conversationId: string): boolean;
  getReadStateVersion(): number;
  getPendingReadMap(): Map<string, ReadStateRecord>;
}

export interface IConversationOperations {
  createConversation(userId: string): Promise<ConversationResponse | null>;
  updateConversation(conversationId: string, updates: Partial<ConversationResponse>): void;
  removeConversation(conversationId: string): void;
  clearConversations(): void;
  updateUnreadCount(conversationId: string, count: number): void;
  incrementUnreadCount(conversationId: string): void;
  setSystemUnreadCount(count: number): void;
  incrementSystemUnreadCount(): void;
  decrementSystemUnreadCount(count?: number): void;
}

export interface IMessageSendService {
  sendMessage(
    conversationId: string,
    segments: any[],
    options?: { replyToId?: string }
  ): Promise<MessageResponse | null>;
}

export interface IMessageSyncService {
  fetchConversations(forceRefresh?: boolean, source?: string): Promise<void>;
  loadMoreConversations(): Promise<void>;
  fetchConversationDetail(conversationId: string): Promise<ConversationResponse | null>;
  fetchMessages(conversationId: string, afterSeq?: number): Promise<void>;
  loadMoreMessages(conversationId: string, beforeSeq: number, limit?: number): Promise<MessageResponse[]>;
  fetchUnreadCount(): Promise<void>;
  canLoadMoreConversations(): boolean;
}

export interface IWSMessageHandler {
  connect(): void;
  disconnect(): void;
  isConnected(): boolean;
}
