/// <reference types="node" />
import type { PlaywrightTestConfig } from '@playwright/test';
import { devices } from '@playwright/test';

/**
 * Configuration for parallel testing with isolated environments
 * Each test suite runs against its own backend instance
 */
const config: PlaywrightTestConfig = {
	testDir: './tests',
	fullyParallel: true, // Enable parallel execution
	forbidOnly: !!process.env.CI,
	retries: process.env.CI ? 2 : 0,
	workers: 3, // Run up to 3 test files in parallel
	reporter: 'html',
	timeout: 30000, // Increased timeout for parallel tests
	expect: {
		timeout: 10000
	},
	use: {
		trace: 'on-first-retry',
		screenshot: 'only-on-failure',
		actionTimeout: 10000
	},
	projects: [
		// Readlist tests
		{
			name: 'readlist-chromium',
			testMatch: /readlist\.test\.ts/,
			use: {
				...devices['Desktop Chrome'],
				baseURL: 'http://localhost:3001'
			}
		},
		{
			name: 'readlist-firefox',
			testMatch: /readlist\.test\.ts/,
			use: {
				...devices['Desktop Firefox'],
				baseURL: 'http://localhost:3001'
			}
		},
		{
			name: 'readlist-webkit',
			testMatch: /readlist\.test\.ts/,
			use: {
				...devices['Desktop Safari'],
				baseURL: 'http://localhost:3001'
			}
		},
		{
			name: 'readlist-mobile-chrome',
			testMatch: /readlist\.test\.ts/,
			use: {
				...devices['Pixel 5'],
				baseURL: 'http://localhost:3001'
			}
		},
		{
			name: 'readlist-mobile-safari',
			testMatch: /readlist\.test\.ts/,
			use: {
				...devices['iPhone 12'],
				baseURL: 'http://localhost:3001'
			}
		},

		// Notes tests
		{
			name: 'notes-chromium',
			testMatch: /notes\.test\.ts/,
			use: {
				...devices['Desktop Chrome'],
				baseURL: 'http://localhost:3002'
			}
		},
		{
			name: 'notes-firefox',
			testMatch: /notes\.test\.ts/,
			use: {
				...devices['Desktop Firefox'],
				baseURL: 'http://localhost:3002'
			}
		},
		{
			name: 'notes-webkit',
			testMatch: /notes\.test\.ts/,
			use: {
				...devices['Desktop Safari'],
				baseURL: 'http://localhost:3002'
			}
		},
		{
			name: 'notes-mobile-chrome',
			testMatch: /notes\.test\.ts/,
			use: {
				...devices['Pixel 5'],
				baseURL: 'http://localhost:3002'
			}
		},
		{
			name: 'notes-mobile-safari',
			testMatch: /notes\.test\.ts/,
			use: {
				...devices['iPhone 12'],
				baseURL: 'http://localhost:3002'
			}
		},

		// Interface tests
		{
			name: 'interface-chromium',
			testMatch: /interface\.test\.ts/,
			use: {
				...devices['Desktop Chrome'],
				baseURL: 'http://localhost:3003'
			}
		},
		{
			name: 'interface-firefox',
			testMatch: /interface\.test\.ts/,
			use: {
				...devices['Desktop Firefox'],
				baseURL: 'http://localhost:3003'
			}
		},
		{
			name: 'interface-webkit',
			testMatch: /interface\.test\.ts/,
			use: {
				...devices['Desktop Safari'],
				baseURL: 'http://localhost:3003'
			}
		},
		{
			name: 'interface-mobile-chrome',
			testMatch: /interface\.test\.ts/,
			use: {
				...devices['Pixel 5'],
				baseURL: 'http://localhost:3003'
			}
		},
		{
			name: 'interface-mobile-safari',
			testMatch: /interface\.test\.ts/,
			use: {
				...devices['iPhone 12'],
				baseURL: 'http://localhost:3003'
			}
		}
	]
};

export default config;