import type { IContact } from '../config.ts';

export type TLegType = 'sip-device' | 'sip-provider' | 'webrtc' | 'tool';
export type TCallDirection = 'inbound' | 'outbound';

export interface IProviderStatus {
  id: string;
  displayName: string;
  registered: boolean;
  publicIp: string | null;
}

export interface IDeviceStatus {
  id: string;
  displayName: string;
  address: string | null;
  port: number;
  aor: string | null;
  connected: boolean;
  isBrowser: boolean;
}

export interface IActiveLeg {
  id: string;
  type: TLegType;
  state: string;
  codec: string | null;
  rtpPort: number | null;
  mediaProtocol: string | null;
  remoteMedia: string | null;
  metadata: Record<string, unknown>;
}

export interface IActiveCall {
  id: string;
  direction: TCallDirection;
  callerNumber: string | null;
  calleeNumber: string | null;
  providerUsed: string | null;
  state: string;
  startedAt: number;
  legs: Map<string, IActiveLeg>;
}

export interface IHistoryLeg {
  id: string;
  type: TLegType;
  state: string;
  codec: string | null;
  rtpPort: number | null;
  remoteMedia: string | null;
  metadata: Record<string, unknown>;
}

export interface ICallHistoryEntry {
  id: string;
  direction: TCallDirection;
  callerNumber: string | null;
  calleeNumber: string | null;
  providerUsed: string | null;
  startedAt: number;
  duration: number;
  legs: IHistoryLeg[];
}

export interface ILegStatus extends IActiveLeg {
  pktSent: number;
  pktReceived: number;
  transcoding: boolean;
}

export interface ICallStatus {
  id: string;
  direction: TCallDirection;
  callerNumber: string | null;
  calleeNumber: string | null;
  providerUsed: string | null;
  state: string;
  startedAt: number;
  duration: number;
  legs: ILegStatus[];
}

export interface IStatusSnapshot {
  instanceId: string;
  uptime: number;
  lanIp: string;
  providers: IProviderStatus[];
  devices: IDeviceStatus[];
  calls: ICallStatus[];
  callHistory: ICallHistoryEntry[];
  contacts: IContact[];
  voicemailCounts: Record<string, number>;
}
