/**
 * Repository 层模型定义
 * 定义应用内部使用的数据模型，与 API 响应和数据库结构解耦
 */

// ==================== 消息模型 ====================

export interface MessageSegment {
  type: string;
  data: Record<string, any>;
}

export interface MessageModel {
  id: string;
  conversationId: string;
  senderId: string;
  content?: string;
  type: 'text' | 'image' | 'file' | 'system' | string;
  isRead: boolean;
  createdAt: Date;
  seq: number;
  status: 'normal' | 'recalled' | 'deleted' | string;
  segments?: MessageSegment[];
  replyToId?: string;
  sender?: UserModel;
}

// ==================== 会话模型 ====================

export type ConversationType = 'private' | 'group';

export interface ConversationModel {
  id: string;
  type: ConversationType;
  participantId: string;
  lastMessageId?: string;
  lastMessage?: MessageModel;
  lastSeq: number;
  myLastReadSeq: number;
  otherLastReadSeq: number;
  unreadCount: number;
  isPinned: boolean;
  participants?: UserModel[];
  group?: GroupModel;
  createdAt: Date;
  updatedAt: Date;
}

// ==================== 用户模型 ====================

export interface UserModel {
  id: string;
  username: string;
  nickname?: string;
  avatar?: string;
  bio?: string;
  website?: string;
  location?: string;
  email?: string;
  phone?: string;
  followersCount?: number;
  followingCount?: number;
  postsCount?: number;
  isFollowing?: boolean;
  isBlocked?: boolean;
  createdAt?: Date;
  updatedAt?: Date;
}

// ==================== 帖子模型 ====================

export interface PostModel {
  id: string;
  authorId: string;
  author?: UserModel;
  title: string;
  content: string;
  images?: string[];
  likeCount: number;
  commentCount: number;
  shareCount: number;
  viewCount: number;
  favoriteCount: number;
  isLiked: boolean;
  isFavorited: boolean;
  isTop: boolean;
  status: 'published' | 'draft' | 'deleted';
  channelId?: string;
  /** 频道展示用（与 PostDTO.channel 一致） */
  channel?: { id: string; name: string } | null;
  tags?: string[];
  createdAt: Date;
  updatedAt: Date;
}

// ==================== 群组模型 ====================

export type GroupMemberRole = 'owner' | 'admin' | 'member';
export type GroupJoinType = 'anyone' | 'approval' | 'invite';

export interface GroupMemberModel {
  userId: string;
  user?: UserModel;
  role: GroupMemberRole;
  nickname?: string;
  joinTime: Date;
  muteUntil?: Date;
  isMuted?: boolean;
}

export interface GroupModel {
  id: string;
  name: string;
  avatar?: string;
  description?: string;
  announcement?: string;
  ownerId: string;
  owner?: UserModel;
  memberCount: number;
  maxMemberCount: number;
  joinType: GroupJoinType;
  isMuted: boolean;
  createdAt: Date;
  updatedAt: Date;
}

// ==================== 分页模型 ====================

export interface PaginatedResult<T> {
  list: T[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
  hasMore: boolean;
}

// ==================== 通用操作结果 ====================

export interface OperationResult<T = void> {
  success: boolean;
  data?: T;
  error?: string;
  code?: string;
}

// ==================== 同步状态模型 ====================

export interface SyncStatus {
  entityType: string;
  entityId: string;
  lastSyncedAt: Date;
  syncVersion: number;
  pendingChanges: boolean;
}
