// playwright.config.ts (or .js)
import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  testDir: './e2e',
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  workers: process.env.CI ? 1 : undefined,
  reporter: 'html',
  use: {
    // This baseURL is for your frontend tests.
    // Tests hitting the backend directly will use absolute URLs.
    baseURL: 'http://localhost:4200',
    trace: 'on-first-retry',
  },

  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },
    {
      name: 'firefox',
      use: { ...devices['Desktop Firefox'] },
    },
  ],

  webServer: {
    command:
      'cd .. && conc -n "frontend,backend" "cd frontend && bun run start" "cd backend/ && watchexec -r -e java ./gradlew :bootRun"',
    // **IMPORTANT CHANGE HERE:**
    // Point to your backend's health check endpoint.
    // If your Spring Boot app uses Actuator, it might be /actuator/health
    // Verify the correct health endpoint for your backend.
    url: 'http://localhost:8080/health', // Or "http://localhost:8080/actuator/health"
    reuseExistingServer: !process.env.CI,
    // **INCREASE TIMEOUT SIGNIFICANTLY**
    // Gradle + Spring Boot can take a while, especially on first run or in CI.
    // Adjust as needed, e.g., 3-5 minutes.
    timeout: 300 * 1000, // 300 seconds = 5 minutes
    stdout: 'pipe', // Good for capturing logs in CI reports
    stderr: 'pipe',
    // Optional: If your server needs specific environment variables
    // env: {
    //   SPRING_PROFILES_ACTIVE: 'test', // Example for Spring Boot
    // },
  },
});
