// vitest.config.e2e.ts
import { defineConfig, mergeConfig } from 'vitest/config';
import type { UserConfig } from 'vite';
import viteConfig from './vite.config';

// Ensure NODE_ENV is set to 'test' for all Vitest runs.
process.env.NODE_ENV = 'test';

// Define a type that includes the 'test' property from Vitest's config.
type ViteConfigWithTest = UserConfig & { test?: UserConfig['test'] };

const { test: _unusedTest, ...baseViteConfig } = viteConfig as ViteConfigWithTest;

/**
 * E2E test configuration.
 * Uses a DIFFERENT port (3098) than integration tests (3099) to allow
 * both test suites to run sequentially without port conflicts.
 */
const e2eConfig = mergeConfig(
  baseViteConfig,
  defineConfig({
    test: {
      name: 'e2e',
      environment: 'node',
      // Point specifically to E2E tests
      include: ['src/tests/e2e/**/*.e2e.test.ts'],
      exclude: [],
      // E2E tests use a different port to avoid conflicts with integration tests
      env: {
        NODE_ENV: 'test',
        BASE_URL: 'https://example.com',
        FRONTEND_URL: 'https://example.com',
        // Use port 3098 for E2E tests (integration uses 3099)
        TEST_PORT: '3098',
        VITE_API_BASE_URL: 'http://localhost:3098/api',
      },
      // E2E tests have their own dedicated global setup file
      globalSetup: './src/tests/setup/e2e-global-setup.ts',
      setupFiles: ['./src/tests/setup/global.ts'],
      // Increase timeout for E2E flows that involve AI or full API chains
      testTimeout: 120000,
      hookTimeout: 60000,
      fileParallelism: false,
      coverage: {
        provider: 'v8',
        // Include 'text' reporter so coverage summary appears after e2e tests complete.
        reporter: [['text', { maxCols: 200 }], 'html', 'json-summary', 'json'],
        reportsDirectory: '.coverage/e2e',
        reportOnFailure: true,
        clean: true,
      },
    },
  }),
);

export default e2eConfig;
