/**
 * Core Room/Session types for reusable WebRTC/WebSocket infrastructure
 * These types are application-agnostic and can be reused across different systems
 */

/**
 * Base Session representing a connected user in a room
 * Contains only infrastructure-level data, no application-specific data
 */
export interface BaseSession {
  // Identity
  id: string;
  userId?: number;
  name: string | null;

  // Connection
  ws?: any; // WebSocket instance
  live: boolean;
  lastActive: number;
  keepAlive?: any;
  connected: boolean;

  // Media
  has_media: boolean; // Whether this session provides audio/video

  // Security
  protected?: boolean; // Whether name is password-protected

  // Bot info (if applicable)
  bot_run_id?: string | null;
  bot_provider_id?: string | null;
  bot_instance_id?: string | null;

  // Internal state (prefixed with _)
  _initialSnapshotSent?: boolean;
  _getBatch?: { fields: Set<string>; timer?: any };
  _pendingMessage?: any;
  _pendingTimeout?: any;
}

/**
 * Generic Session with application-specific metadata
 * Use this to extend BaseSession with your application data
 */
export interface Session<TMetadata = any> extends BaseSession {
  metadata?: TMetadata; // Application-specific data goes here
}

/**
 * Participant information for room listing
 * This is what gets sent to clients for the participant list
 */
export interface Participant {
  name: string | null;
  session_id: string;
  live: boolean;
  protected?: boolean;
  has_media: boolean;
  bot_run_id?: string | null;
  bot_provider_id?: string | null;
  bot_instance_id?: string | null;
  muted?: boolean;
  video_on?: boolean;
}

/**
 * Base Room representing a multi-user session space
 * Contains only infrastructure-level data
 */
export interface BaseRoom {
  // Identity
  id: string;
  name: string;

  // Sessions
  sessions: Record<string, BaseSession>;

  // State
  state: string; // e.g., "lobby", "active", "completed"
  created: number;
  lastActivity: number;

  // Flags
  private?: boolean;
}

/**
 * Generic Room with application-specific metadata
 * Use this to extend BaseRoom with your application data
 */
export interface Room<TMetadata = any> extends BaseRoom {
  metadata?: TMetadata; // Application-specific data goes here
}

/**
 * Message types for room communication
 */
export type IncomingMessage = {
  type: string | null;
  data: any;
  field?: string;
  value?: any;
  fields?: string[];
};

/**
 * WebRTC peer configuration
 */
export interface PeerConfig {
  ws: any;
  has_media: boolean;
  hasAudio?: boolean; // Legacy
  hasVideo?: boolean; // Legacy
}

/**
 * Audio/Video peer registry for a room
 */
export type PeerRegistry = Record<string, PeerConfig>;
