// vitest.config.integration.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';

// 1. Separate the 'test' config (which has Unit Test settings) 
//    from the rest of the general Vite config (plugins, aliases, etc.)
// DEBUG: Use console.error to ensure logs appear in CI/CD output
console.error('[DEBUG] Loading vitest.config.integration.ts...');

// Define a type that includes the 'test' property from Vitest's config.
// This allows us to destructure it in a type-safe way without using 'as any'.
type ViteConfigWithTest = UserConfig & { test?: UserConfig['test'] };

const { test: _unusedTest, ...baseViteConfig } = viteConfig as ViteConfigWithTest;
console.error('[DEBUG] Merging with base vite config, but excluding its "test" configuration.');

console.error('\n[DEBUG] --- INTEGRATION CONFIG SETUP ---');
// Use _unusedTest to satisfy linter
console.error(`[DEBUG] Stripped "test" config. Original test config existed: ${!!_unusedTest}`);
// Use the 'in' operator for a type-safe property check instead of casting to 'any'.
console.error('[DEBUG] Does baseViteConfig have "test"?', 'test' in baseViteConfig);
console.error('[DEBUG] Base vite config keys:', Object.keys(baseViteConfig));

/**
 * This configuration is specifically for integration tests.
 * It MERGES with the main vite.config.ts to inherit plugins and aliases,
 * then overrides the test-specific settings for a Node.js environment.
 */
const finalConfig = mergeConfig(baseViteConfig, defineConfig({
  test: {
    // Override settings from the main config for this specific test project.
    name: 'integration',
    environment: 'node',
    // Point specifically to the new integration tests directory.
    // This pattern will match any test file inside `src/tests/integration/`.
    include: ['src/tests/integration/**/*.test.{ts,tsx}'],
    // CRITICAL: We must override the `exclude` property from the base vite.config.ts.
    // Otherwise, the inherited `exclude` rule will prevent any integration tests from running.
    // Setting it to an empty array removes all exclusion rules for this project.
    exclude: [],
    // This setup script starts the backend server before tests run.
    globalSetup: './src/tests/setup/integration-global-setup.ts',
    // The default timeout is 5000ms (5 seconds)
    testTimeout: 60000, // Increased timeout for server startup and API calls, especially AI services.
    // "singleThread: true" is removed in modern Vitest.
    // Use fileParallelism: false to ensure test files run one by one to prevent port conflicts.
    fileParallelism: false,
    coverage: {
      provider: 'v8',
      // We remove 'text' here. The final text report will be generated by `nyc` after merging.
      reporter: ['html', 'json-summary', 'json'],
      reportsDirectory: '.coverage/integration',
      reportOnFailure: true, // This ensures the report generates even if tests fail
      clean: true,
    },
  }
}));

console.error('[DEBUG] Integration Final Config - INCLUDE:', finalConfig.test?.include);
console.error('[DEBUG] Integration Final Config - EXCLUDE:', finalConfig.test?.exclude);
console.error('[DEBUG] ----------------------------------\n');

export default finalConfig;