/**
 * 简化环境配置模块
 *
 * 按照constitution要求，提供统一的环境变量管理
 */

/**
 * 配置管理类
 */
export class Config {
  /**
   * 获取环境变量值
   */
  static get(key: string, defaultValue?: string): string | undefined {
    return process.env[key] || defaultValue
  }

  /**
   * 获取必需的环境变量值
   */
  static getRequired(key: string): string {
    const value = process.env[key]
    if (!value) {
      throw new Error(`Required environment variable ${key} is not set`)
    }
    return value
  }

  /**
   * 获取布尔型环境变量
   */
  static getBoolean(key: string, defaultValue = false): boolean {
    const value = process.env[key]
    if (!value) return defaultValue
    return ['true', '1', 'yes', 'on'].includes(value.toLowerCase())
  }

  /**
   * 获取数字型环境变量
   */
  static getNumber(key: string, defaultValue?: number): number | undefined {
    const value = process.env[key]
    if (!value) return defaultValue
    const numValue = Number(value)
    return isNaN(numValue) ? defaultValue : numValue
  }

  /**
   * 获取代理配置
   */
  static getProxyConfig() {
    return {
      enabled: this.getBoolean('PROXY_ENABLED', false),
      protocol: this.get('PROXY_PROTOCOL', 'http'),
      host: this.get('PROXY_HOST'),
      port: this.getNumber('PROXY_PORT'),
      username: this.get('PROXY_USERNAME'),
      password: this.get('PROXY_PASSWORD'),
    }
  }

  /**
   * 获取超时配置
   */
  static getTimeoutConfig() {
    return {
      connect: this.getNumber('TIMEOUT_CONNECT', 5000),
      read: this.getNumber('TIMEOUT_READ', 30000),
      write: this.getNumber('TIMEOUT_WRITE', 15000),
    }
  }

  /**
   * 获取日志配置
   */
  static getLoggingConfig() {
    return {
      level: this.get('LOG_LEVEL', 'info'),
      logSensitiveData: this.getBoolean('LOG_SENSITIVE_DATA', false),
    }
  }

  /**
   * 检查是否为开发环境
   */
  static isDev(): boolean {
    const nodeEnv = this.get('NODE_ENV', 'development')
    return nodeEnv === 'development' || nodeEnv === 'dev'
  }

  /**
   * 代理配置管理
   */
  static proxy = {
    /**
     * 检查是否配置了任何代理
     */
    isAnyConfigured(): boolean {
      return Config.proxy.isConfigured() || Config.proxy.aster.isConfigured()
    },

    /**
     * 检查全局代理是否配置
     */
    isConfigured(): boolean {
      return !!(Config.get('PROXY_HOST') && Config.getNumber('PROXY_PORT'))
    },

    /**
     * 获取代理协议
     */
    protocol(): string {
      return Config.get('PROXY_PROTOCOL', 'http')
    },

    /**
     * 获取代理主机
     */
    host(): string | undefined {
      return Config.get('PROXY_HOST')
    },

    /**
     * 获取代理端口
     */
    port(): number | undefined {
      return Config.getNumber('PROXY_PORT')
    },

    /**
     * 获取代理用户名
     */
    username(): string | undefined {
      return Config.get('PROXY_USERNAME')
    },

    /**
     * 获取代理密码
     */
    password(): string | undefined {
      return Config.get('PROXY_PASSWORD')
    },

    /**
     * 获取代理URL（根据交易所选择不同代理）
     */
    getUrl(exchange?: 'aster' | 'pacifica' | 'binance'): string | undefined {
      if (exchange === 'aster' && Config.proxy.aster.isConfigured()) {
        const { protocol, host, port, username, password } = Config.proxy.aster
        const auth = username() && password() ? `${username()}:${password()}@` : ''
        return `${protocol()}://${auth}${host()}:${port()}`
      }

      if (Config.proxy.isConfigured()) {
        const auth =
          Config.proxy.username() && Config.proxy.password()
            ? `${Config.proxy.username()}:${Config.proxy.password()}@`
            : ''
        return `${Config.proxy.protocol()}://${auth}${Config.proxy.host()}:${Config.proxy.port()}`
      }

      return undefined
    },

    /**
     * Aster专用代理配置
     */
    aster: {
      isConfigured(): boolean {
        return !!(Config.get('ASTER_PROXY_HOST') && Config.getNumber('ASTER_PROXY_PORT'))
      },

      protocol(): string {
        return Config.get('ASTER_PROXY_PROTOCOL', 'http')
      },

      host(): string | undefined {
        return Config.get('ASTER_PROXY_HOST')
      },

      port(): number | undefined {
        return Config.getNumber('ASTER_PROXY_PORT')
      },

      username(): string | undefined {
        return Config.get('ASTER_PROXY_USER')
      },

      password(): string | undefined {
        return Config.get('ASTER_PROXY_PASSWORD')
      },

      sessionPrefix(): string | undefined {
        return Config.get('ASTER_PROXY_SESSION_PREFIX')
      },

      sessionSuffix(): string | undefined {
        return Config.get('ASTER_PROXY_SESSION_SUFFIX')
      },
    },
  }
}
