/**
 * Platform Adapter Types
 *
 * Core types for platform-specific HTTP adapters that integrate with the credential manager
 * and provide unified interface for different trading platforms.
 */

export interface IPlatformAdapter {
  /** Platform identifier */
  readonly platform: string

  /** Platform base URL */
  readonly baseUrl: string

  /**
   * Execute HTTP request
   * @param request Platform-specific request
   * @returns Platform response
   */
  request<T = any>(request: PlatformRequest): Promise<PlatformResponse<T>>

  /**
   * Prepare request with authentication and formatting
   * @param request Original request
   * @returns Prepared request with authentication
   */
  prepareRequest(request: PlatformRequest): Promise<PreparedRequest>

  /**
   * Set proxy configuration
   * @param proxyConfig Proxy configuration or null to disable
   */
  setProxyConfig(proxyConfig: ProxyConfig | null): Promise<void>

  /**
   * Get current proxy status
   * @returns Proxy status information
   */
  getProxyStatus(): Promise<ProxyStatus>

  /**
   * Process raw response
   * @param response Raw response from platform
   * @returns Processed platform response
   */
  processResponse<T>(response: RawResponse): Promise<PlatformResponse<T>>

  /**
   * Check platform health status
   * @returns Health status
   */
  checkHealth(): Promise<PlatformHealthStatus>

  /**
   * Get platform configuration
   * @returns Platform configuration
   */
  getConfig(): PlatformConfig

  /**
   * Validate platform configuration
   * @returns Whether configuration is valid
   */
  validateConfig(): boolean
}

export interface PlatformRequest {
  /** Account identifier */
  accountId: string

  /** HTTP method */
  method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'

  /** Request path (relative to baseUrl) */
  path: string

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

  /** Request body */
  body?: any

  /** Query parameters */
  params?: Record<string, any>

  /** Platform-specific request options */
  options?: PlatformRequestOptions
}

export interface PlatformRequestOptions {
  /** Whether authentication is required */
  requiresAuth?: boolean

  /** Request timeout in milliseconds */
  timeout?: number

  /** Whether to enable retry logic */
  enableRetry?: boolean
}

export interface PreparedRequest extends PlatformRequest {
  /** Whether request was authenticated */
  authenticated: boolean

  /** Signature algorithm used */
  signatureAlgorithm?: string

  /** Whether credential manager was used */
  credentialManagerUsed?: boolean

  /** Whether signature was verified */
  signatureVerified?: boolean
}

export interface PlatformResponse<T = any> {
  /** HTTP status code */
  status: number

  /** Response data */
  data: T

  /** Response headers */
  headers: Record<string, string>

  /** Whether request was successful */
  success: boolean

  /** Error information if failed */
  error?: PlatformError

  /** Response metadata */
  metadata: PlatformResponseMetadata
}

export interface PlatformResponseMetadata {
  /** Platform identifier */
  platform: string

  /** Request identifier */
  requestId: string

  /** Request duration in milliseconds */
  duration: number

  /** Response timestamp */
  timestamp: Date

  /** HTTP status code */
  statusCode: number

  /** Response size in bytes */
  responseSize: number

  /** Whether resources were cleaned up */
  resourcesCleanedUp?: boolean
}

export interface PlatformError {
  /** Error type */
  type: 'client_error' | 'server_error' | 'network_error' | 'auth_error'

  /** Error message */
  message: string

  /** Error code */
  code: string
}

export interface RawResponse {
  /** HTTP status code */
  status: number

  /** Response headers */
  headers: Record<string, string>

  /** Raw response body */
  body: string | any

  /** Whether request was successful */
  ok: boolean
}

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

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

  /** Proxy host */
  host: string

  /** Proxy port */
  port: number

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

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

  /** Proxy host */
  host?: string

  /** Proxy port */
  port?: number
}

export interface PlatformHealthStatus {
  /** Platform identifier */
  platform: string

  /** Health status */
  status: 'healthy' | 'degraded' | 'unhealthy'

  /** Response time in milliseconds */
  responseTime?: number

  /** Last health check timestamp */
  lastCheck: Date
}

export interface PlatformConfig {
  /** Platform identifier */
  platform: string

  /** Base URL for API */
  baseUrl: string

  /** Authentication configuration */
  authConfig: AuthConfig

  /** Timeout configuration */
  timeouts?: TimeoutConfig
}

export interface AuthConfig {
  /** Authentication type */
  type: 'signature' | 'apikey' | 'bearer'

  /** Signature algorithm for signature-based auth */
  algorithm?: 'Ed25519' | 'HMAC-SHA256' | 'EIP-191'
}

export interface TimeoutConfig {
  /** Connection timeout in milliseconds */
  connect: number

  /** Read timeout in milliseconds */
  read: number

  /** Write timeout in milliseconds */
  write: number
}

// Type aliases
export type PlatformType = 'pacifica' | 'binance' | 'aster'