/**
 * @fileoverview Configuration for the OpenClaw API client.
 *
 * Centralizes gateway URL, auth token, WebSocket options, and polling intervals.
 * Designed for Angular environment injection — values can be overridden at build time
 * or via Angular's environment.ts files.
 */

export interface OpenClawClientConfig {
  /** Gateway HTTP base URL, e.g. "http://10.60.1.145:18789" */
  gatewayUrl: string;

  /** Gateway WebSocket URL, e.g. "ws://10.60.1.145:18789" */
  gatewayWsUrl: string;

  /** Auth token (from OPENCLAW_GATEWAY_TOKEN or configured secret) */
  authToken: string;

  /** Auth mode — must match gateway.auth.mode in openclaw.json */
  authMode: 'token' | 'password' | 'none' | 'trusted-proxy';

  /** How often (ms) to poll for session status when WS is not connected */
  pollIntervalMs: number;

  /** How often (ms) to poll for agent list refresh */
  agentListPollMs: number;

  /** WebSocket reconnect delay (ms), with exponential backoff */
  wsReconnectDelayMs: number;

  /** Maximum WebSocket reconnect attempts before giving up */
  wsMaxReconnectAttempts: number;

  /** Request timeout for HTTP calls (ms) */
  httpTimeoutMs: number;

  /** Protocol version for WS handshake */
  protocolVersion: number;

  /** Client identity for WS handshake */
  clientId: string;

  /** Client version for WS handshake */
  clientVersion: string;

  /** Client platform for WS handshake */
  clientPlatform: string;
}

/**
 * Default configuration — targets the LAN-bound gateway on port 18789.
 * Override specific fields via Angular environment injection.
 */
export const DEFAULT_CONFIG: OpenClawClientConfig = {
  gatewayUrl: 'http://10.60.1.145:18789',
  gatewayWsUrl: 'ws://10.60.1.145:18789',
  authToken: '',
  authMode: 'token',
  pollIntervalMs: 5_000,
  agentListPollMs: 30_000,
  wsReconnectDelayMs: 1_000,
  wsMaxReconnectAttempts: 50,
  httpTimeoutMs: 15_000,
  protocolVersion: 3,
  clientId: 'control-center',
  clientVersion: '0.1.0',
  clientPlatform: 'web',
};

/**
 * Create a resolved config by merging partial overrides onto defaults.
 */
export function createConfig(overrides: Partial<OpenClawClientConfig> = {}): OpenClawClientConfig {
  return { ...DEFAULT_CONFIG, ...overrides };
}