// ─── User types ────────────────────────────────────────────────────────

export interface UserBasic {
  id: string;
  userName: string;
  displayName: string;
  avatarUrl: string | null;
  // Fallbacks for compatibility
  username?: string;
  avatar?: string | null;
}

export interface UserPresence extends UserBasic {
  isOnline: boolean;
  lastSeen: string;
}

export interface User extends UserPresence {
  bio: string | null;
  birthday: string | null;
  createdAt: string;
  hideStoryViews?: boolean;
}

// ─── Chat types ────────────────────────────────────────────────────────

export interface ChatMember {
  id: string;
  userId: string;
  role: string;
  isPinned?: boolean;
  isMuted?: boolean;
  isArchived?: boolean;
  clearedAt?: string | null;
  user: UserPresence;
}

export interface MediaItem {
  id: string;
  type: string;
  url: string;
  filename: string | null;
  thumbnail: string | null;
  size: number | null;
  duration: number | null;
  width?: number | null;
  height?: number | null;
}

export interface Reaction {
  id: string;
  emoji: string;
  userId: string;
  user: {
    id: string;
    username: string;
    userName?: string;
    displayName: string;
    avatar?: string | null;
    avatarUrl?: string | null;
  };
}

export interface MessageSender {
  id: string;
  username: string;
  userName?: string;
  displayName: string;
  avatar?: string | null;
  avatarUrl?: string | null;
}

export interface Message {
  id: string;
  chatId: string;
  senderId: string;
  content: string | null;
  type: string;
  replyToId: string | null;
  quote?: string | null;
  forwardedFromId?: string | null;
  storyId?: string | null;
  storyMediaUrl?: string | null;
  storyMediaType?: string | null;
  isEdited: boolean;
  isDeleted: boolean;
  isDeletedForUser?: boolean;
  scheduledAt?: string | null;
  createdAt: string;
  updatedAt?: string;
  sequenceId: number;
  sender: MessageSender;
  replyTo?: {
    id: string;
    content: string | null;
    isDeleted?: boolean;
    quote?: string | null;
    media?: MediaItem[];
    sender: {
      id: string;
      username: string;
      userName?: string;
      displayName: string;
      avatar?: string | null;
      avatarUrl?: string | null;
    };
  } | null;
  forwardedFrom?: UserBasic | null;
  media: MediaItem[];
  reactions: Reaction[];
  readBy: Array<{ userId: string }>;
  callType?: 'voice' | 'video' | string | null;
  callStatus?: 'missed' | 'completed' | 'cancelled' | 'declined' | string | null;
  duration?: number | null;
  pollOptions?: Array<{ id: string; text: string; voteCount: number; voters?: Array<{ id: string; username: string; displayName: string; avatar?: string | null }>; voterIds?: string[] }>;
  pollIsMultipleChoice?: boolean;
  pollIsClosed?: boolean;
  pollIsAnonymous?: boolean;
  userVotedOptionIds?: string[];
}

export interface Chat {
  id: string;
  type: 'personal' | 'group' | 'channel' | 'favorites';
  name: string | null;
  description?: string | null;
  avatar: string | null;
  avatarUrl?: string | null;
  createdAt: string;
  members: ChatMember[];
  messages: Message[];
  unreadCount: number;
  isImporting?: boolean;
  importJobId?: string | null;
  pinnedMessages?: Array<{
    id: string;
    message: Message;
  }>;
}

// ─── Socket event types ────────────────────────────────────────────────

export interface TypingUser {
  chatId: string;
  userId: string;
}

export interface CallInfo {
  from: string;
  offer: RTCSessionDescriptionInit;
  callType: 'voice' | 'video';
  chatId: string;
  callerInfo?: UserBasic | null;
}

// ─── Story types ───────────────────────────────────────────────────────

export interface Story {
  id: string;
  type: string;
  mediaUrl: string | null;
  content: string | null;
  bgColor: string | null;
  isMuted?: boolean;
  createdAt: string;
  expiresAt: string;
  viewCount: number;
  viewed: boolean;
  reactions?: Array<{ id: string; userId: string; emoji: string; createdAt: string }>;
  replyCount?: number;
}

export interface StoryViewer {
  userId: string;
  username: string;
  displayName: string;
  avatar: string | null;
  viewedAt: string;
}

export interface StoryGroup {
  user: UserBasic;
  stories: Story[];
  hasUnviewed: boolean;
}

// ─── Friend types ──────────────────────────────────────────────────

export interface FriendRequest {
  id: string;
  user: User;
  createdAt: string;
  isOutgoing?: boolean;
}

export interface FriendWithId extends UserPresence {
  friendshipId: string;
}

export interface FriendshipStatus {
  status: 'none' | 'pending' | 'accepted' | 'declined' | 'self';
  friendshipId?: string | null;
  direction?: 'incoming' | 'outgoing';
}

// ─── Utility types ─────────────────────────────────────────────────────

/** Audio file extensions recognized by the app. */
export const AUDIO_EXTENSIONS = ['.mp3', '.wav', '.ogg', '.m4a', '.aac', '.flac', '.wma'] as const;

/** Max file size for uploads (200MB). */
export const MAX_FILE_SIZE = 200 * 1024 * 1024;

/** Max avatar size (5MB). */
export const MAX_AVATAR_SIZE = 5 * 1024 * 1024;
