import { ILogger } from "./types";

export class ContextManager {
  private context: Record<string, any> = {};

  constructor(private logger?: ILogger) {}

  setInitialContext(initialContext: Record<string, any>): void {
    this.context = { ...initialContext };
    this.logger?.info('Initial context set', { contextKeys: Object.keys(initialContext) });
  }

  updateContext(newContext: Record<string, any>): void {
    this.context = { ...this.context, ...newContext };
    this.logger?.debug('Context updated', { updatedKeys: Object.keys(newContext) });
  }

  getContext(): Record<string, any> {
    return { ...this.context };
  }

  getFormattedContext(): string {
    return Object.entries(this.context)
      .map(([key, value]) => `${key}: ${JSON.stringify(value)}`)
      .join('\n');
  }

  clearContext(): void {
    this.context = {};
    this.logger?.info('Context cleared');
  }

  getContextValue(key: string): any {
    const value = this.context[key];
    if (value === undefined) {
      this.logger?.warn(`Attempted to access undefined context key: ${key}`);
    }
    return value;
  }

  setContextValue(key: string, value: any): void {
    this.context[key] = value;
    this.logger?.debug(`Context value set for key: ${key}`);
  }
}