/**
 * 浏览器提供商抽象基类
 * 所有浏览器提供商必须实现此接口
 */
class BaseBrowserProvider {
  constructor(config = {}) {
    this.config = config;
    this.browser = null;
    this.page = null;
  }

  /**
   * 获取提供商名称
   * @returns {string}
   */
  getName() {
    throw new Error('getName() must be implemented by subclass');
  }

  /**
   * 是否为免费提供商
   * @returns {boolean}
   */
  isFree() {
    throw new Error('isFree() must be implemented by subclass');
  }

  /**
   * 获取提供商能力
   * @returns {Object} { stealth, fingerprint, proxy, incognito, ... }
   */
  getCapabilities() {
    throw new Error('getCapabilities() must be implemented by subclass');
  }

  /**
   * 启动浏览器
   * @param {Object} options - 启动选项
   * @returns {Promise<Object>} { browser, page, wsEndpoint }
   */
  async launch(options = {}) {
    throw new Error('launch() must be implemented by subclass');
  }

  /**
   * 连接到现有浏览器
   * @param {string} wsEndpoint - WebSocket endpoint
   * @returns {Promise<Object>} { browser, page }
   */
  async connect(wsEndpoint) {
    throw new Error('connect() must be implemented by subclass');
  }

  /**
   * 关闭浏览器
   * @returns {Promise<void>}
   */
  async close() {
    throw new Error('close() must be implemented by subclass');
  }

  /**
   * 清除浏览器缓存
   * @returns {Promise<void>}
   */
  async clearCache() {
    throw new Error('clearCache() must be implemented by subclass');
  }

  /**
   * 创建新页面
   * @returns {Promise<Page>}
   */
  async newPage() {
    throw new Error('newPage() must be implemented by subclass');
  }

  /**
   * 获取当前浏览器实例
   * @returns {Browser|null}
   */
  getBrowser() {
    return this.browser;
  }

  /**
   * 获取当前页面实例
   * @returns {Page|null}
   */
  getPage() {
    return this.page;
  }

  /**
   * 设置用户代理
   * @param {string} userAgent
   * @returns {Promise<void>}
   */
  async setUserAgent(userAgent) {
    throw new Error('setUserAgent() must be implemented by subclass');
  }

  /**
   * 设置视口大小
   * @param {Object} viewport - { width, height }
   * @returns {Promise<void>}
   */
  async setViewport(viewport) {
    throw new Error('setViewport() must be implemented by subclass');
  }

  /**
   * 执行JavaScript代码
   * @param {string} script
   * @returns {Promise<any>}
   */
  async evaluate(script) {
    if (!this.page) {
      throw new Error('No page available');
    }
    return await this.page.evaluate(script);
  }

  /**
   * 获取配置信息
   * @returns {Object}
   */
  getConfig() {
    return this.config;
  }

  /**
   * 验证配置
   * @returns {Promise<boolean>}
   */
  async validateConfig() {
    return true;
  }

  /**
   * 获取提供商元数据
   * @returns {Object}
   */
  getMetadata() {
    return {
      name: this.getName(),
      free: this.isFree(),
      capabilities: this.getCapabilities(),
      version: this.getVersion ? this.getVersion() : '1.0.0'
    };
  }
}

module.exports = BaseBrowserProvider;
