import { MCPAdapter, MCPToolResult } from '../benchmark-runner';

export interface OpenChromeAdapterOptions {
  mode: 'ax' | 'dom';
  serverUrl?: string; // for future remote server support
}

/**
 * Stub MCP Adapter for benchmark CI mode.
 * Returns mock responses without connecting to a real MCP server.
 * Use for deterministic tool call count and regression testing.
 */
export class OpenChromeStubAdapter implements MCPAdapter {
  name = 'OpenChrome';
  mode: string;
  private options: OpenChromeAdapterOptions;
  private tabSeq = 0;

  private _totalInputChars = 0;
  private _totalOutputChars = 0;
  private _toolCallCount = 0;

  constructor(options: OpenChromeAdapterOptions) {
    this.options = options;
    this.mode = options.mode;
  }

  async setup(): Promise<void> {
    this.resetMetrics();
  }

  async teardown(): Promise<void> {
    // No-op for stub
  }

  async callTool(toolName: string, args: Record<string, unknown>): Promise<MCPToolResult> {
    const inputJson = JSON.stringify({ tool: toolName, args });
    this._totalInputChars += inputJson.length;

    // Stub response - no actual MCP server connection
    const text = toolName === 'tabs_create'
      ? JSON.stringify({ tabId: `stub-${this.mode}-tab-${++this.tabSeq}` })
      : 'stub response';
    const result: MCPToolResult = {
      content: [{ type: 'text', text }],
    };

    const outputJson = JSON.stringify(result);
    this._totalOutputChars += outputJson.length;
    this._toolCallCount += 1;

    return result;
  }

  get totalInputChars(): number {
    return this._totalInputChars;
  }

  get totalOutputChars(): number {
    return this._totalOutputChars;
  }

  get toolCallCount(): number {
    return this._toolCallCount;
  }

  resetMetrics(): void {
    this._totalInputChars = 0;
    this._totalOutputChars = 0;
    this._toolCallCount = 0;
    this.tabSeq = 0;
  }
}

/**
 * @deprecated Use OpenChromeStubAdapter instead.
 * Kept for backward compatibility.
 */
export const OpenChromeAdapter = OpenChromeStubAdapter;
export type OpenChromeAdapter = OpenChromeStubAdapter;
