import { defineConfig, devices } from '@playwright/test';
import dotenv from 'dotenv';

/**
 * Read environment variables from file.
 * https://github.com/motdotla/dotenv
 */
dotenv.config({ path: '.env' });

/**
 * See https://playwright.dev/docs/test-configuration.
 */
export default defineConfig({
  testDir: './tests',
  /* Run tests in files in parallel */
  fullyParallel: false,  // Disabled to prevent cart state conflicts
  /* Fail the build on CI if you accidentally left test.only in the source code. */
  forbidOnly: !!process.env.CI,
  /* Retry on CI only */
  retries: process.env.CI ? 2 : 0,
  /* Opt out of parallel tests to prevent cart state conflicts
   * Tests run sequentially (one at a time) in alphabetical order:
   * 01-authentication, 02-core-commerce, 03-user-management, etc.
   */
  workers: 1,
  /* Increase test timeout for AJAX-heavy pages */
  timeout: 60000,  // 60 seconds per test
  /* Reporter to use. See https://playwright.dev/docs/test-reporters */
  reporter: [['html'], ['json', { outputFile: 'test-results.json' }]],
  /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
  use: {
    /* Base URL to use in actions like `await page.goto('')`. */
    baseURL: process.env.SHOPFLOW_URL || 'https://shopflow.php8',

    /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
    trace: 'on-first-retry',
    
    /* Take screenshot on failure */
    screenshot: 'only-on-failure',
    
    /* Record video on failure */
    video: 'retain-on-failure',
    
    /* Timeout settings */
    actionTimeout: 10000,
    navigationTimeout: 30000,
    
    /* Ignore HTTPS errors for self-signed certificates */
    ignoreHTTPSErrors: true,
  },

  /* Configure projects for major browsers
   * Projects with dependencies run in dependency order.
   * Phase projects depend on each other to enforce sequential execution (01->02->03->04->05->06).
   * Chromium project depends on all phases, so running --project=chromium runs all phases in order.
   */
  projects: [
    // Phase 1: Authentication (must run first - no dependencies)
    {
      name: '01-authentication',
      testMatch: /01-authentication\/.*\.spec\.ts/,
      use: { ...devices['Desktop Chrome'] },
    },
    // Phase 2: Core Commerce (depends on Phase 1)
    {
      name: '02-core-commerce',
      testMatch: /02-core-commerce\/.*\.spec\.ts/,
      dependencies: ['01-authentication'],
      use: { ...devices['Desktop Chrome'] },
    },
    // Phase 3: User Management (depends on Phase 2)
    {
      name: '03-user-management',
      testMatch: /03-user-management\/.*\.spec\.ts/,
      dependencies: ['02-core-commerce'],
      use: { ...devices['Desktop Chrome'] },
    },
    // Phase 4: Partner Management (depends on Phase 3)
    {
      name: '04-partner-management',
      testMatch: /04-partner-management\/.*\.spec\.ts/,
      dependencies: ['03-user-management'],
      use: { ...devices['Desktop Chrome'] },
    },
    // Phase 5: Price Management (depends on Phase 4)
    {
      name: '05-price-management',
      testMatch: /05-price-management\/.*\.spec\.ts/,
      dependencies: ['04-partner-management'],
      use: { ...devices['Desktop Chrome'] },
    },
    // Phase 6: Seller Portal (depends on Phase 5)
    {
      name: '06-seller-portal',
      testMatch: /06-seller-portal\/.*\.spec\.ts/,
      dependencies: ['05-price-management'],
      use: { ...devices['Desktop Chrome'] },
    },
    // Chromium project - depends on Phase 6, which cascades to all previous phases
    // Running --project=chromium will execute all phases in order (01->02->03->04->05->06)
    // No testMatch means this project only runs its dependencies, ensuring sequential execution
    {
      name: 'chromium',
      dependencies: ['06-seller-portal'],
      use: { ...devices['Desktop Chrome'] },
    },

    /* Other browsers - can be enabled if needed
    {
      name: 'firefox',
      testMatch: /.*\.spec\.ts/,
      use: { ...devices['Desktop Firefox'] },
    },

    {
      name: 'webkit',
      testMatch: /.*\.spec\.ts/,
      use: { ...devices['Desktop Safari'] },
    },
    */

    /* Test against mobile viewports. */
    // {
    //   name: 'Mobile Chrome',
    //   use: { ...devices['Pixel 5'] },
    // },
    // {
    //   name: 'Mobile Safari',
    //   use: { ...devices['iPhone 12'] },
    // },

    /* Test against branded browsers. */
    // {
    //   name: 'Microsoft Edge',
    //   use: { ...devices['Desktop Edge'], channel: 'msedge' },
    // },
    // {
    //   name: 'Google Chrome',
    //   use: { ...devices['Desktop Chrome'], channel: 'chrome' },
    // },
  ],

  /* Run your local dev server before starting the tests */
  // webServer: {
  //   command: 'npm run start',
  //   url: 'http://localhost:3000',
  //   reuseExistingServer: !process.env.CI,
  // },
});
