/** JSON-only contract for fetching ephemeral WebRTC ICE configuration. */

import type { ScreenShareBinding, ScreenShareCredential } from "./signalingTypes.ts";

export const ICE_CONFIG_PROTOCOL_VERSION = 1 as const;

export interface IceConfigRequest {
  type: "ice-config-request";
  protocolVersion: typeof ICE_CONFIG_PROTOCOL_VERSION;
  requestId: string;
  binding: ScreenShareBinding;
  credential: ScreenShareCredential;
}

export interface StunIceServer {
  urls: readonly string[];
}

export interface TurnIceServer {
  urls: readonly string[];
  username: string;
  credential: string;
  credentialType: "password";
}

export interface IceConfigGrant {
  type: "ice-config-grant";
  protocolVersion: typeof ICE_CONFIG_PROTOCOL_VERSION;
  requestId: string;
  issuedAtMs: number;
  expiresAtMs: number;
  iceServers: readonly (StunIceServer | TurnIceServer)[];
}

export interface IceConfigUnavailable {
  type: "ice-config-unavailable";
  protocolVersion: typeof ICE_CONFIG_PROTOCOL_VERSION;
  requestId: string;
  retryAfterMs: number;
}

export type IceConfigResponse = IceConfigGrant | IceConfigUnavailable;

export type IceConfigValidationResult<T> =
  | { ok: true; value: T }
  | { ok: false; error: string };
