import { test as base, expect, Page } from '@playwright/test';
import { TestDataManager } from './utils/test-data-manager';
import { ApiClient } from './utils/api-client';

/**
 * Extended test fixture with v1.0.0 features
 */
export type TestFixtures = {
  testData: TestDataManager;
  apiClient: ApiClient;
  authenticatedPage: Page;
  scenarioPage: Page;
  comparisonPage: Page;
};

/**
 * Test data interface for type safety
 */
export interface TestUser {
  id?: string;
  email: string;
  password: string;
  fullName: string;
  apiKey?: string;
}

export interface TestScenario {
  id?: string;
  name: string;
  description: string;
  region: string;
  tags: string[];
  status?: string;
}

export interface TestReport {
  id?: string;
  scenarioId: string;
  format: 'pdf' | 'csv';
  includeLogs: boolean;
}

/**
 * Extended test with fixtures
 */
export const test = base.extend<TestFixtures>({
  // Test data manager
  testData: async ({}, use) => {
    const manager = new TestDataManager();
    await use(manager);
    await manager.cleanup();
  },

  // API client
  apiClient: async ({}, use) => {
    const client = new ApiClient(process.env.TEST_BASE_URL || 'http://localhost:8000');
    await use(client);
  },

  // Pre-authenticated page
  authenticatedPage: async ({ page, testData }, use) => {
    // Create test user
    const user = await testData.createTestUser();
    
    // Navigate to login
    await page.goto('/login');
    
    // Perform login
    await page.fill('[data-testid="email-input"]', user.email);
    await page.fill('[data-testid="password-input"]', user.password);
    await page.click('[data-testid="login-button"]');
    
    // Wait for dashboard
    await page.waitForURL('/dashboard');
    await expect(page.locator('[data-testid="dashboard-header"]')).toBeVisible();
    
    await use(page);
  },

  // Scenario management page
  scenarioPage: async ({ authenticatedPage }, use) => {
    await authenticatedPage.goto('/scenarios');
    await expect(authenticatedPage.locator('[data-testid="scenarios-list"]')).toBeVisible();
    await use(authenticatedPage);
  },

  // Comparison page
  comparisonPage: async ({ authenticatedPage }, use) => {
    await authenticatedPage.goto('/compare');
    await expect(authenticatedPage.locator('[data-testid="comparison-page"]')).toBeVisible();
    await use(authenticatedPage);
  },
});

export { expect };
