/**
 * Universal HTTP Client Core Types
 *
 * Core interface and types for the Universal HTTP Client that provides
 * unified access to multiple trading platform APIs with credential management integration.
 */

import type { HttpClientRequest, HttpClientResponse, BatchRequestOptions, BatchResult } from './httpClient'
import type { IPlatformAdapter, PlatformConfig } from './platformAdapter'

export interface IUniversalHttpClient {
  /**
   * Execute HTTP request
   * @param request Request configuration
   * @returns Response data
   */
  request<T = any>(request: HttpClientRequest): Promise<HttpClientResponse<T>>

  /**
   * Execute batch HTTP requests
   * @param requests Array of request configurations
   * @param options Batch execution options
   * @returns Batch execution result
   */
  batchRequest<T = any>(
    requests: HttpClientRequest[],
    options?: BatchRequestOptions
  ): Promise<BatchResult<T>>

  /**
   * Register new platform adapter
   * @param platform Platform name
   * @param adapter Platform adapter instance
   */
  registerPlatform(platform: string, adapter: IPlatformAdapter): void

  /**
   * Get client health status
   * @returns Health status information
   */
  getHealth(): Promise<HealthStatus>

  /**
   * Close client and release resources
   */
  close(): Promise<void>

  // Optional extension methods for platform management

  /**
   * Check if platform is registered
   * @param platform Platform name
   * @returns Whether platform is registered
   */
  isPlatformRegistered?(platform: string): boolean

  /**
   * Get platform adapter
   * @param platform Platform name
   * @returns Platform adapter instance
   */
  getPlatformAdapter?(platform: string): IPlatformAdapter | undefined

  /**
   * Get list of registered platforms
   * @returns Array of registered platform names
   */
  getRegisteredPlatforms?(): string[]

  /**
   * Unregister platform
   * @param platform Platform name
   * @returns Operation result
   */
  unregisterPlatform?(platform: string): Promise<{ success: boolean }>

  /**
   * Update platform configuration
   * @param platform Platform name
   * @param config Partial configuration update
   * @returns Operation result
   */
  updatePlatformConfig?(platform: string, config: Partial<PlatformConfig>): Promise<{ success: boolean }>

  /**
   * Get platform information
   * @param platform Platform name
   * @returns Platform configuration
   */
  getPlatformInfo?(platform: string): PlatformConfig | undefined

  /**
   * Configure global proxy
   * @param config Proxy configuration
   * @returns Operation result
   */
  configureGlobalProxy?(config: GlobalProxyConfig): Promise<ProxyConfigurationResult>

  /**
   * Configure account-specific proxy
   * @param accountId Account identifier
   * @param config Proxy configuration
   * @returns Operation result
   */
  configureAccountProxy?(accountId: string, config: AccountProxyConfig): Promise<ProxyConfigurationResult>
}

export interface HealthStatus {
  /** Overall health status */
  status: 'healthy' | 'degraded' | 'unhealthy'

  /** Timestamp of health check */
  timestamp: Date

  /** Overall response time */
  responseTime?: number

  /** Platform-specific health status */
  platforms: Record<string, PlatformHealthInfo>

  /** Active connections count */
  activeConnections?: number

  /** Memory usage information */
  memoryUsage?: {
    used: number
    total: number
    percentage: number
  }

  /** Error information if unhealthy */
  errors?: HealthError[]
}

export interface PlatformHealthInfo {
  /** Platform health status */
  status: 'healthy' | 'degraded' | 'unhealthy'

  /** Response time for this platform */
  responseTime?: number

  /** Last successful request timestamp */
  lastSuccess?: Date

  /** Error count in recent period */
  errorCount?: number

  /** Success rate (0-1) */
  successRate?: number
}

export interface HealthError {
  /** Error message */
  message: string

  /** Platform where error occurred */
  platform?: string

  /** Error timestamp */
  timestamp: Date

  /** Error severity */
  severity: 'low' | 'medium' | 'high' | 'critical'
}

export interface GlobalProxyConfig {
  /** Whether proxy is enabled */
  enabled: boolean

  /** Proxy URL */
  url?: string

  /** Proxy host */
  host?: string

  /** Proxy port */
  port?: number

  /** Proxy protocol */
  protocol?: 'http' | 'https' | 'socks4' | 'socks5'

  /** Proxy authentication */
  auth?: {
    username: string
    password: string
  }

  /** Custom headers */
  headers?: Record<string, string>

  /** Proxy timeout */
  timeout?: number

  /** Health check configuration */
  healthCheck?: {
    enabled: boolean
    interval: number
    timeout: number
    retries: number
  }
}

export interface AccountProxyConfig extends GlobalProxyConfig {
  /** Account identifier */
  accountId: string

  /** Priority over global proxy */
  priority?: number

  /** Session stickiness */
  stickySession?: boolean
}

export interface ProxyConfigurationResult {
  /** Whether configuration was successful */
  success: boolean

  /** Proxy URL if configured */
  proxyUrl?: string

  /** Whether authentication was configured */
  authConfigured?: boolean

  /** Whether custom headers were configured */
  customHeaders?: boolean

  /** Account ID for account-specific configuration */
  accountId?: string

  /** Error message if failed */
  error?: string
}

// Re-export commonly used types for convenience
export type {
  HttpClientRequest,
  HttpClientResponse,
  BatchRequestOptions,
  BatchResult,
  IPlatformAdapter,
  PlatformConfig
}