/**
 * Shared type definitions for the WebRTC subsystem.
 */

/** Tracks a single peer's connection, data channel, and RTP senders. */
export interface PeerData {
  /** The underlying RTCPeerConnection instance. */
  connection: RTCPeerConnection;
  /** The negotiated data channel, or `null` before the channel is established. */
  dataChannel: RTCDataChannel | null;
  /** Timestamp (ms since epoch) when this peer attempt was created. */
  createdAt: number;
  /** `true` when this side created the offer (and data channel). */
  isInitiator: boolean;
  /** ICE candidates received before the remote description was set. */
  pendingIceCandidates: RTCIceCandidateInit[];
  /** The RTP sender carrying the local audio track. */
  audioSender?: RTCRtpSender;
  /** The RTP sender carrying the local video (camera) track. */
  videoSender?: RTCRtpSender;
  /** The RTP sender carrying the screen-share video track. */
  screenVideoSender?: RTCRtpSender;
  /** The RTP sender carrying the screen-share audio track. */
  screenAudioSender?: RTCRtpSender;
  /** Known remote stream ids that carry the peer's voice audio. */
  remoteVoiceStreamIds: Set<string>;
  /** Known remote stream ids that carry the peer's camera video. */
  remoteCameraStreamIds: Set<string>;
  /** Known remote stream ids that carry the peer's screen-share audio/video. */
  remoteScreenShareStreamIds: Set<string>;
}

/** Credentials cached for automatic re-identification after reconnect. */
export interface IdentifyCredentials {
  /** The user's unique order / peer identifier. */
  oderId: string;
  /** Session token proving identity to the signaling server. */
  token: string;
  /** The user's display name shown to other peers. */
  displayName: string;
  /** Optional profile description advertised via signaling identity. */
  description?: string;
  /** Monotonic profile version for late-join reconciliation. */
  profileUpdatedAt?: number;
  /** Public signal-server URL where this user registered. */
  homeSignalServerUrl?: string;
  /** Stable per-install client id used for multi-device session routing. */
  clientInstanceId?: string;
}

/** Last-joined server info, used for reconnection. */
export interface JoinedServerInfo {
  /** The server (room) that was last joined. */
  serverId: string;
  /** The local user ID at the time of joining. */
  userId: string;
}

/** Entry in the disconnected-peer tracker for P2P reconnect scheduling. */
export interface DisconnectedPeerEntry {
  /** Timestamp (ms since epoch) when the peer was last seen connected. */
  lastSeenTimestamp: number;
  /** Number of reconnect attempts made so far. */
  reconnectAttempts: number;
}

/** Snapshot of current voice / screen state (broadcast to peers). */
export interface VoiceStateSnapshot {
  /** Whether the user's voice is currently active. */
  isConnected: boolean;
  /** Whether the user's microphone is muted. */
  isMuted: boolean;
  /** Whether the user has self-deafened. */
  isDeafened: boolean;
  /** Whether the user is sharing their screen. */
  isScreenSharing: boolean;
  /** The voice channel room ID, if applicable. */
  roomId?: string;
  /** The voice channel server ID, if applicable. */
  serverId?: string;
  /** Install-scoped client id that owns active voice for this snapshot. */
  clientInstanceId?: string;
}
