/**
 * Crypto.com OTC 2.0 API Configuration
 * Based on: https://exchange-docs.crypto.com/exchange/v1/rest-ws/index_OTC2.html
 */

// ============================================================================
// Environment Configuration
// ============================================================================

export interface EnvironmentConfig {
  restBaseUrl: string;
  wsUrl: string;
}

export const ENVIRONMENTS: Record<string, EnvironmentConfig> = {
  production: {
    restBaseUrl: 'https://api.crypto.com/exchange/v1',
    wsUrl: 'wss://stream.crypto.com/exchange/v1/user',
  },
  uat: {
    restBaseUrl: 'https://uat-api.3ona.co/exchange/v1',
    wsUrl: 'wss://uat-stream.3ona.co/exchange/v1/user',
  },
};

// ============================================================================
// Rate Limits (requests per second)
// ============================================================================

export const REST_RATE_LIMITS: Record<string, number> = {
  'get-otc-instruments': 1,
  'get-open-quote-requests': 1,
  'get-open-quotes': 1,
  'get-quote-request-history': 1,
  'get-quote-history': 1,
  'get-open-deals': 1,
  'get-deal-history': 1,
  'get-settle-later-limit': 1,
  'get-unsettled-amounts': 1,
};

export const WEBSOCKET_RATE_LIMITS: Record<string, number> = {
  'request-quote': 2,
  'request-deal': 2,
  'respond-quote': 25, // 20-30 per second (LP only)
};

// ============================================================================
// Quote Request Configuration
// ============================================================================

export const QUOTE_DURATIONS = [
  '5000',
  '10000',
  '30000',
  '60000',
  '300000',
  '600000',
] as const;

export const QUOTE_TTL_OPTIONS = ['4000', '5000'] as const;

export const SETTLEMENT_ARRANGEMENTS = ['IMMEDIATE', 'T1'] as const;

// ============================================================================
// API Methods
// ============================================================================

export const API_METHODS = {
  // Authentication
  AUTH: 'public/auth',
  HEARTBEAT_RESPONSE: 'public/respond-heartbeat',

  // Reference Data
  GET_OTC_INSTRUMENTS: 'private/otc/get-otc-instruments',

  // RFQ (Request for Quote)
  REQUEST_QUOTE: 'private/otc/request-quote',
  REQUEST_DEAL: 'private/otc/request-deal',

  // Quote Query
  GET_OPEN_QUOTE_REQUESTS: 'private/otc/get-open-quote-requests',
  GET_OPEN_QUOTES: 'private/otc/get-open-quotes',
  GET_QUOTE_REQUEST_HISTORY: 'private/otc/get-quote-request-history',
  GET_QUOTE_HISTORY: 'private/otc/get-quote-history',

  // Deal Query
  GET_OPEN_DEALS: 'private/otc/get-open-deals',
  GET_DEAL_HISTORY: 'private/otc/get-deal-history',

  // Settle Later
  GET_SETTLE_LATER_LIMIT: 'private/otc/get-settle-later-limit',
  GET_UNSETTLED_AMOUNTS: 'private/otc/get-unsettled-amounts',
} as const;

// ============================================================================
// WebSocket Channels
// ============================================================================

export const WS_CHANNELS = {
  QUOTE_REQUESTS: 'user.otc_qr.requests',
  QUOTES: 'user.otc_qr.quotes',
  DEALS: 'user.otc.deals',
} as const;

// ============================================================================
// Default Configuration Values
// ============================================================================

export const DEFAULT_CONFIG = {
  environment: 'production' as const,
  requestTimeout: 10000, // 10 seconds
  reconnectInterval: 5000, // 5 seconds
  heartbeatInterval: 30000, // 30 seconds
  connectionDelay: 1000, // 1 second delay after connection per Crypto.com docs
};

// ============================================================================
// Validation Constants
// ============================================================================

export const VALIDATION = {
  // Accepted characters for IDs: a-z, A-Z, 0-9, _, -
  ID_PATTERN: /^[a-zA-Z0-9_-]+$/,
  
  // Maximum open quote requests per user
  MAX_OPEN_QUOTE_REQUESTS: 50,
  
  // Page size limits
  DEFAULT_PAGE_SIZE: 20,
  MAX_PAGE_SIZE: 200,
};

// ============================================================================
// Response Codes
// ============================================================================

export const RESPONSE_CODES = {
  SUCCESS: 0,
  TOO_MANY_REQUESTS: 10007,
  INVALID_NONCE: 10001,
} as const;

// ============================================================================
// Helper Functions
// ============================================================================

export function getEnvironmentConfig(environment: 'production' | 'uat'): EnvironmentConfig {
  return ENVIRONMENTS[environment];
}

export function isValidClientId(id: string): boolean {
  return VALIDATION.ID_PATTERN.test(id);
}
