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

/**
 * Playwright configuration for ClawFort UI/UX regression testing
 * @see https://playwright.dev/docs/test-configuration
 */
export default defineConfig({
	testDir: "./tests",

	/* Run tests in files in parallel */
	fullyParallel: true,

	/* 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 on CI for stability */
	workers: process.env.CI ? 1 : undefined,

	/* Reporter to use. See https://playwright.dev/docs/test-reporters */
	reporter: [
		["html", { open: "never" }],
		["list"],
		["junit", { outputFile: "test-results/junit.xml" }],
	],

	/* 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.BASE_URL || "http://localhost:8000",

		/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
		trace: "on-first-retry",

		/* Capture screenshot on failure */
		screenshot: "only-on-failure",

		/* Record video on failure */
		video: "on-first-retry",

		/* Viewport defaults */
		viewport: { width: 1280, height: 720 },

		/* Action timeout */
		actionTimeout: 15000,

		/* Navigation timeout */
		navigationTimeout: 30000,
	},

	/* Configure projects for major browsers and viewports */
	projects: [
		// Smoke tests - fast feedback
		{
			name: "smoke-chromium",
			use: {
				...devices["Desktop Chrome"],
				viewport: { width: 1280, height: 720 },
			},
			grep: /@smoke/,
		},

		// Full regression - Desktop
		{
			name: "desktop-chrome",
			use: {
				...devices["Desktop Chrome"],
				viewport: { width: 1280, height: 720 },
			},
		},
		{
			name: "desktop-firefox",
			use: {
				...devices["Desktop Firefox"],
				viewport: { width: 1280, height: 720 },
			},
		},
		{
			name: "desktop-webkit",
			use: {
				...devices["Desktop Safari"],
				viewport: { width: 1280, height: 720 },
			},
		},

		// Tablet
		{
			name: "tablet-chrome",
			use: {
				...devices["Desktop Chrome"],
				viewport: { width: 768, height: 1024 },
			},
		},
		{
			name: "tablet-webkit",
			use: {
				...devices["Desktop Safari"],
				viewport: { width: 768, height: 1024 },
			},
		},

		// Mobile
		{
			name: "mobile-chrome",
			use: {
				...devices["Pixel 5"],
			},
		},
		{
			name: "mobile-safari",
			use: {
				...devices["iPhone 12"],
			},
		},
	],

	/* Run local dev server before starting the tests */
	webServer: {
		command: "cd .. && python -m backend.main",
		url: "http://localhost:8000",
		reuseExistingServer: !process.env.CI,
		timeout: 120000,
	},
});
