/**
 * Types for Matrix RTC calling functionality
 * Uses LiveKit via the org.matrix.msc4143.rtc_foci configuration
 */

/**
 * LiveKit focus configuration from well-known
 */
export interface LiveKitFocus {
  type: 'livekit';
  livekit_service_url: string;
}

/**
 * RTC Foci configuration from well-known response
 */
export type RtcFoci = LiveKitFocus;

/**
 * Extended well-known response with RTC configuration
 */
export interface WellKnownWithRTC {
  'm.homeserver': {
    base_url: string;
  };
  'm.identity_server'?: {
    base_url: string;
  };
  'io.element.e2ee'?: {
    default?: boolean;
    force_disable?: boolean;
  };
  'org.matrix.msc4143.rtc_foci'?: RtcFoci[];
}

/**
 * OpenID token response from the Matrix homeserver
 */
export interface OpenIdToken {
  access_token: string;
  token_type: string;
  matrix_server_name: string;
  expires_in: number;
}

/**
 * Member information for LiveKit JWT request
 */
export interface LiveKitMember {
  id: string;
  claimed_user_id: string;
  claimed_device_id: string;
}

/**
 * Request body for LiveKit JWT service
 */
export interface LiveKitJWTRequest {
  openid_token: OpenIdToken;
  room_id: string;
  member: LiveKitMember;
}

/**
 * LiveKit JWT response from the service
 */
export interface LiveKitJWTResponse {
  jwt: string;
  url: string;
}

/**
 * Possible call states
 */
export enum CallState {
  Idle = 'idle',
  Connecting = 'connecting',
  Connected = 'connected',
  Reconnecting = 'reconnecting',
  Disconnecting = 'disconnecting',
  Ended = 'ended',
  Error = 'error',
}

/**
 * Call type
 */
export enum CallType {
  Voice = 'voice',
  Video = 'video',
}

/**
 * Screen share track info with the actual track for proper attachment
 */
export interface ScreenShareTrackInfo {
  participantId: string;
  track: unknown; // RemoteTrack from livekit-client
  stream: MediaStream;
}

/**
 * Active call information
 */
export interface ActiveCall {
  roomId: string;
  callType: CallType;
  state: CallState;
  startTime: number;
  localStream?: MediaStream;
  localScreenStream?: MediaStream;
  remoteStreams: Map<string, MediaStream>;
  remoteScreenStreams: Map<string, MediaStream>;
  remoteScreenTracks: Map<string, ScreenShareTrackInfo>;
  participants: string[];
  activeSpeakers: Set<string>;
  mutedParticipants: Set<string>;
  isMuted: boolean;
  isDeafened: boolean;
  isVideoEnabled: boolean;
  isScreenSharing: boolean;
  /** The homeserver being used for LiveKit JWT (if different from user's homeserver) */
  livekitHomeserver?: string;
}

/**
 * Call event types for the call service
 */
export enum CallEventType {
  StateChanged = 'state_changed',
  ParticipantJoined = 'participant_joined',
  ParticipantLeft = 'participant_left',
  RemoteStreamAdded = 'remote_stream_added',
  RemoteStreamRemoved = 'remote_stream_removed',
  ScreenShareStarted = 'screen_share_started',
  ScreenShareStopped = 'screen_share_stopped',
  SpeakersChanged = 'speakers_changed',
  RemoteMuteChanged = 'remote_mute_changed',
  Error = 'error',
}

/**
 * Call event payload
 */
export interface CallEvent {
  type: CallEventType;
  roomId: string;
  data?: unknown;
}

/**
 * Call service configuration
 */
export interface CallServiceConfig {
  /** @deprecated No longer used - JWT fetched dynamically from multiple homeservers */
  livekitServiceUrl?: string;
  homeserverBaseUrl: string;
  accessToken: string;
  userId: string;
  deviceId: string;
}

/**
 * Feed information for call member event
 */
export interface CallFeed {
  purpose: 'usermedia' | 'screenshare';
}

/**
 * Device information for call member event
 */
export interface CallDevice {
  device_id: string;
  session_id: string;
  expires_ts: number;
  feeds: CallFeed[];
}

/**
 * Call information for call member event
 */
export interface CallInfo {
  'm.call_id': string;
  'm.devices': CallDevice[];
}

/**
 * Content for org.matrix.msc3401.call.member state event
 */
export interface CallMemberEventContent {
  'm.calls': CallInfo[];
}
