// vite.config.ts
import path from 'path';
import { defineConfig } from 'vitest/config';
import react from '@vitejs/plugin-react';

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

/**
 * This is the main configuration file for Vite and the Vitest 'unit' test project.
 * When running `vitest`, it is orchestrated by `vitest.workspace.ts`, which
 * separates the unit and integration test environments.
 */
export default defineConfig({
  // Vite-specific configuration for the dev server, build, etc.
  // This is inherited by all Vitest projects.
  plugins: [react()],
  server: {
    port: 3000,
    host: '0.0.0.0',
  },
  resolve: {
    alias: {
      // Use __dirname for a more robust path resolution
      '@': path.resolve(__dirname, './src'),      
      // This alias ensures that any import of 'services/logger' is resolved
      // to the browser-safe client version during the Vite build process.
      // Server-side code should explicitly import 'services/logger.server'.
      'services/logger': path.resolve(__dirname, './src/services/logger.client.ts'),
    },
  },

  // Vitest-specific configuration for the 'unit' test project.
  test: {
    // Name this project 'unit' to distinguish it in the workspace.
    name: 'unit',
    // By default, Vitest does not suppress console logs.
    // The onConsoleLog hook is only needed if you want to conditionally filter specific logs.
    // Keeping the default behavior is often safer to avoid missing important warnings.

    environment: 'jsdom',
    // Explicitly point Vitest to the correct tsconfig and enable globals.
    globals: true, // tsconfig is auto-detected, so the explicit property is not needed and causes an error.
    globalSetup: './src/tests/setup/global-setup.ts',
    setupFiles: ['./src/tests/setup/tests-setup-unit.ts'],
    // Explicitly include only test files.
    // We remove 'src/vite-env.d.ts' which was causing it to be run as a test.
    include: ['src/**/*.test.{ts,tsx}'],
    // Exclude integration tests and other non-test files from the unit test runner.
    exclude: [
      '**/node_modules/**',
      '**/dist/**',
      'src/tests/integration/**', // Exclude the entire integration test directory
      '**/*.e2e.test.ts'
    ],
    // Disable file parallelism to run tests sequentially (replaces --no-threads)
    fileParallelism: false,
    coverage: {
      provider: 'v8',
      // We remove 'text' here. The final text report will be generated by `nyc` after merging.
      reporter: [
        // Add maxCols to suggest a wider output for the text summary.
        ['text', { maxCols: 200 }],
        'html', 'json'],
      // hanging-process reporter helps identify tests that do not exit properly - comes at a high cost tho
      //reporter: ['verbose', 'html', 'json', 'hanging-process'],
      reportsDirectory: './.coverage/unit', 
      clean: true,
      reportOnFailure: true, // This ensures the report generates even if tests fail
      include: ['src/**/*.{ts,tsx}'],
      // Refine exclusions to be more comprehensive
      // By excluding scripts, setup files, and type definitions, we get a more accurate
      // picture of the test coverage for the actual application logic.
      exclude: [
        'src/index.tsx',                  // Application entry point
        'src/main.tsx',                   // A common alternative entry point name
        'src/types.ts',
        'src/tests/**', // Exclude all test setup and helper files
        'src/vitest.setup.ts',            // Global test setup config
        'src/**/*.test.{ts,tsx}', // Exclude test files themselves
        'src/**/*.stories.{ts,tsx}', // Exclude Storybook stories
        'src/**/*.d.ts', // Exclude type definition files
        'src/components/icons/**', // Exclude icon components if they are simple wrappers
        'src/db/seed.ts',                 // Database seeding script
        'src/db/seed_admin_account.ts',   // Database seeding script
        'src/db/backup_user.ts',          // Database backup script
      ],
    },
  },
});