import { BrowserContext, Page } from '@playwright/test';
import { TestUser, Quest, ChatMessage, BargainingContext } from '../types';

/**
 * Fixtures and context shared across all E2E tests.
 * This serves as the "world" object in Cucumber terms.
 */
export class TestContext {
  public page!: Page;
  public context!: BrowserContext;
  public baseUrl: string;
  public currentUser?: TestUser;
  public quests: Map<string, Quest> = new Map();
  public chatMessages: ChatMessage[] = [];
  public bargainingContext?: BargainingContext;
  public notificationsData: Map<string, any> = new Map();
  public suggestedAction?: any;
  public lastApiResponse?: Response;
  public goldAmount: number = 500;

  constructor(baseUrl: string = process.env.SUT_URL || 'http://localhost') {
    this.baseUrl = baseUrl;
  }

  /**
   * Set the current page for interactions
   */
  setPage(page: Page): void {
    this.page = page;
  }

  /**
   * Set the browser context for managing cookies/storage
   */
  setContext(context: BrowserContext): void {
    this.context = context;
  }

  /**
   * Log in a test user
   */
  setCurrentUser(user: TestUser): void {
    this.currentUser = user;
  }

  /**
   * Store a quest for reference
   */
  addQuest(quest: Quest): void {
    this.quests.set(quest.id || quest.title, quest);
  }

  /**
   * Add a chat message to the transcript
   */
  addChatMessage(message: ChatMessage): void {
    this.chatMessages.push(message);
  }

  /**
   * Clear chat transcript
   */
  clearChatMessages(): void {
    this.chatMessages = [];
  }

  /**
   * Reset context for a new test
   */
  reset(): void {
    this.currentUser = undefined;
    this.quests.clear();
    this.chatMessages = [];
    this.bargainingContext = undefined;
    this.notificationsData.clear();
    this.suggestedAction = undefined;
    this.lastApiResponse = undefined;
  }
}
