import { HistoryEntry, ILogger } from "./types/common";

export class RequestHistory {
  private history: HistoryEntry[] = [];
  private maxLength: number;

  constructor(maxLength: number = 5, private logger?: ILogger) {
    this.maxLength = maxLength;
  }

  addEntry(request: string, response: string): void {
    const entry: HistoryEntry = {
      request,
      response,
      timestamp: Date.now(),
    };

    this.history.unshift(entry);

    if (this.history.length > this.maxLength) {
      this.history.pop();
    }

    this.logger?.debug(`Added new entry to request history. Current history length: ${this.history.length}`);
  }

  getHistory(): HistoryEntry[] {
    return [...this.history];
  }

  clear(): void {
    this.history = [];
    this.logger?.info('Request history cleared');
  }

  getFormattedHistory(): string {
    return this.history.map(entry => 
      `User: ${entry.request}\nAI: ${entry.response}\n`
    ).join('\n');
  }
}