import type { TLegType } from './status.ts';

export interface IIncomingCallEvent {
  call_id: string;
  from_uri: string;
  to_number: string;
  provider_id: string;
  ring_browsers?: boolean;
}

export interface IOutboundCallEvent {
  call_id: string;
  from_device: string | null;
  to_number: string;
}

export interface IOutboundCallStartedEvent {
  call_id: string;
  number: string;
  provider_id: string;
  ring_browsers?: boolean;
}

export interface ICallRingingEvent {
  call_id: string;
}

export interface ICallAnsweredEvent {
  call_id: string;
  provider_media_addr?: string;
  provider_media_port?: number;
  media_protocol?: string;
  sip_pt?: number;
}

export interface ICallEndedEvent {
  call_id: string;
  reason: string;
  duration: number;
  from_side?: string;
}

export interface IProviderRegisteredEvent {
  provider_id: string;
  registered: boolean;
  public_ip: string | null;
}

export interface IDeviceRegisteredEvent {
  device_id: string;
  display_name: string;
  address: string;
  port: number;
  aor: string;
  expires: number;
}

export interface ISipUnhandledEvent {
  method_or_status: string;
  call_id?: string;
  from_addr: string;
  from_port: number;
}

export interface ILegAddedEvent {
  call_id: string;
  leg_id: string;
  kind: TLegType;
  state: string;
  codec?: string | null;
  rtpPort?: number | null;
  mediaProtocol?: string | null;
  remoteMedia?: string | null;
  metadata?: Record<string, unknown>;
}

export interface ILegRemovedEvent {
  call_id: string;
  leg_id: string;
}

export interface ILegStateChangedEvent {
  call_id: string;
  leg_id: string;
  state: string;
  codec?: string | null;
  rtpPort?: number | null;
  mediaProtocol?: string | null;
  remoteMedia?: string | null;
  metadata?: Record<string, unknown>;
}

export interface IWebRtcIceCandidateEvent {
  session_id: string;
  candidate: string;
  sdp_mid?: string;
  sdp_mline_index?: number;
}

export interface IWebRtcStateEvent {
  session_id?: string;
  state: string;
}

export interface IWebRtcTrackEvent {
  session_id?: string;
  kind: string;
  codec: string;
}

export interface IWebRtcAudioRxEvent {
  session_id?: string;
  packet_count: number;
}

export interface IVoicemailStartedEvent {
  call_id: string;
  voicebox_id?: string;
  caller_number?: string;
}

export interface IRecordingDoneEvent {
  call_id?: string;
  voicebox_id?: string;
  file_path: string;
  duration_ms: number;
  caller_number?: string;
}

export interface IVoicemailErrorEvent {
  call_id: string;
  error: string;
}

export interface IFaxStartedEvent {
  call_id: string;
  leg_id: string;
  direction: 'outbound' | 'inbound';
  transport: 'audio' | 't38';
  file_path: string;
  fax_box_id?: string;
  caller_number?: string;
  codec?: string;
  remote_media?: string;
}

export interface IFaxCompletedEvent {
  call_id: string;
  leg_id: string;
  direction: 'outbound' | 'inbound';
  transport: 'audio' | 't38';
  file_path: string;
  fax_box_id?: string;
  caller_number?: string;
  codec?: string;
  success: boolean;
  completion_code?: number | null;
  completion_label?: string | null;
  stats: {
    bit_rate: number;
    error_correcting_mode: boolean;
    pages_tx: number;
    pages_rx: number;
    image_size: number;
    bad_rows: number;
    longest_bad_row_run: number;
    ecm_retries: number;
    current_status: number;
    rtp_events: number;
    rtn_events: number;
  };
}

export interface IFaxFailedEvent {
  call_id: string;
  leg_id: string;
  direction: 'outbound' | 'inbound';
  transport: 'audio' | 't38';
  file_path: string;
  fax_box_id?: string;
  caller_number?: string;
  error: string;
}

export type TProxyEventMap = {
  provider_registered: IProviderRegisteredEvent;
  device_registered: IDeviceRegisteredEvent;
  incoming_call: IIncomingCallEvent;
  outbound_device_call: IOutboundCallEvent;
  outbound_call_started: IOutboundCallStartedEvent;
  call_ringing: ICallRingingEvent;
  call_answered: ICallAnsweredEvent;
  call_ended: ICallEndedEvent;
  sip_unhandled: ISipUnhandledEvent;
  leg_added: ILegAddedEvent;
  leg_removed: ILegRemovedEvent;
  leg_state_changed: ILegStateChangedEvent;
  webrtc_ice_candidate: IWebRtcIceCandidateEvent;
  webrtc_state: IWebRtcStateEvent;
  webrtc_track: IWebRtcTrackEvent;
  webrtc_audio_rx: IWebRtcAudioRxEvent;
  voicemail_started: IVoicemailStartedEvent;
  recording_done: IRecordingDoneEvent;
  voicemail_error: IVoicemailErrorEvent;
  fax_started: IFaxStartedEvent;
  fax_completed: IFaxCompletedEvent;
  fax_failed: IFaxFailedEvent;
};
