import { defineConfig } from 'vitest/config';
import react from '@vitejs/plugin-react';
import path from 'path';
import { fileURLToPath } from 'url';
import { storybookTest } from '@storybook/addon-vitest/vitest-plugin';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: [{
      find: /^~\/database\/(.*)$/,
      replacement: path.resolve(__dirname, './database/$1')
    }, {
      find: /^~\/server\/(.*)$/,
      replacement: path.resolve(__dirname, './server/$1')
    }, {
      find: /^~\/(.*)$/,
      replacement: path.resolve(__dirname, './app/$1')
    }, {
      find: /^@\/(.*)$/,
      replacement: path.resolve(__dirname, './app/$1')
    }, {
      find: 'app',
      replacement: path.resolve(__dirname, './app')
    }]
  },
  test: {
    coverage: {
      provider: 'v8',
      reporter: ['text', 'json', 'html'],
      exclude: ['node_modules/', 'app/test/', '**/*.test.{ts,tsx}', '**/*.spec.{ts,tsx}', '**/types.ts']
    },
    projects: [{
      extends: true,
      test: {
        name: 'unit',
        globals: true,
        environment: 'jsdom',
        setupFiles: ['./app/test/setup.ts']
      }
    }, {
      extends: true,
      plugins: [storybookTest({ configDir: path.join(__dirname, '.storybook') })],
      test: {
        name: 'storybook',
        browser: {
          enabled: true,
          headless: true,
          provider: 'playwright',
          instances: [{ browser: 'chromium' }]
        }
      }
    }]
  }
});
