/**
 * Mock LLM provider — for testing.
 *
 * Returns pre-configured responses or a default echo response.
 */

import type { ChatCompletionRequest, ChatCompletionResponse, LLMProvider } from '../types.js';

export class MockLLMProvider implements LLMProvider {
  private responses: ChatCompletionResponse[] = [];
  public calls: ChatCompletionRequest[] = [];

  constructor(responses?: ChatCompletionResponse[]) {
    if (responses) this.responses = [...responses];
  }

  isConfigured(): boolean {
    return true;
  }

  /** Add a response to the queue. */
  addResponse(response: ChatCompletionResponse): void {
    this.responses.push(response);
  }

  async chatCompletion(req: ChatCompletionRequest): Promise<ChatCompletionResponse> {
    this.calls.push(req);

    if (this.responses.length > 0) {
      return this.responses.shift()!;
    }

    // Default echo response
    const lastMessage = req.messages[req.messages.length - 1];
    return {
      content: `Mock response to: ${lastMessage?.content ?? '(empty)'}`,
      model: req.model ?? 'mock-model',
      finishReason: 'stop',
      usage: { promptTokens: 10, completionTokens: 10, totalTokens: 20 },
    };
  }

  /** Reset call history and responses. */
  reset(): void {
    this.calls = [];
    this.responses = [];
  }
}
