import { defineConfig, devices } from '@playwright/test';
import dotenv from 'dotenv';

dotenv.config(); // Loads E2E_ENTRA_USERNAME, E2E_ENTRA_PASSWORD, TEST_DATABASE_URL

export default defineConfig({
  testDir: './e2e',
  workers: 1, // Keep sequential so PostgreSQL table truncations don't collide
  use: {
    baseURL: 'http://localhost:4200',
    trace: 'on-first-retry',
  },

  projects: [
    // 1. Runs once to log into Microsoft Entra ID and save state
    {
      name: 'setup',
      testMatch: /.*\.setup\.ts/,
    },
    // 2. Runs all E2E tests using the saved Entra ID session
    {
      name: 'chromium',
      use: {
        ...devices['Desktop Chrome'],
        storageState: 'playwright/.auth/user.json',
      },
      dependencies: ['setup'],
    },
  ],

  // Automatically start FastAPI (pointed at test DB) and Angular before tests run
  webServer: [
    {
      command: 'uvicorn app.main:app --port 8000',
      url: 'http://localhost:8000/docs',
      reuseExistingServer: !process.env.CI,
      env: {
        DATABASE_URL: process.env.TEST_DATABASE_URL!,
      },
    },
    {
      command: 'npm run start',
      url: 'http://localhost:4200',
      reuseExistingServer: !process.env.CI,
    },
  ],
});

