import js from '@eslint/js';
import typescriptEslint from '@typescript-eslint/eslint-plugin';
import typescriptParser from '@typescript-eslint/parser';
import reactPlugin from 'eslint-plugin-react';
import reactHooks from 'eslint-plugin-react-hooks';
import globals from 'globals';

export default [
  // 基础配置
  {
    files: ['**/*.{js,jsx,ts,tsx}'],
    ignores: [
      'dist/**',
      'node_modules/**',
      '*.config.js',
      '*.config.ts',
      'tests/e2e/**',
      'scripts/**',
      'server.js',
      'vitest.config.components.ts',
      'coverage/**',
    ],
    languageOptions: {
      ecmaVersion: 'latest',
      sourceType: 'module',
      parser: typescriptParser,
      parserOptions: {
        ecmaFeatures: {
          jsx: true,
        },
      },
      globals: {
        ...globals.browser,
        ...globals.es2021,
        RequestInfo: 'readonly',
        RequestInit: 'readonly',
        URL: 'readonly',
        Response: 'readonly',
      },
    },
    plugins: {
      '@typescript-eslint': typescriptEslint,
      react: reactPlugin,
      'react-hooks': reactHooks,
    },
    rules: {
      // 基础ESLint规则
      ...js.configs.recommended.rules,

      // TypeScript规则
      '@typescript-eslint/no-unused-vars': ['error', {
        argsIgnorePattern: '^_',
        varsIgnorePattern: '^_',
        caughtErrorsIgnorePattern: '^_',
      }],
      '@typescript-eslint/no-explicit-any': 'warn',
      '@typescript-eslint/explicit-function-return-type': 'off',

      // React规则
      'react/react-in-jsx-scope': 'off',
      'react/prop-types': 'off',

      // 通用规则
      'no-console': 'warn',
      'prefer-const': 'error',
      'no-undef': 'off', // TypeScript已经处理了未定义变量
      'no-unused-vars': 'off', // 使用TypeScript的版本
    },
    settings: {
      react: {
        version: 'detect',
      },
    },
  },

  // Node.js环境配置
  {
    files: ['src/server/**/*.{js,ts}', 'src/test/**/*.{js,ts}'],
    languageOptions: {
      globals: {
        ...globals.node,
      },
    },
  },

  // 测试环境配置
  {
    files: ['src/**/__tests__/**/*.{js,ts,jsx,tsx}', 'src/**/__integration_tests__/**/*.{js,ts,jsx,tsx}'],
    languageOptions: {
      globals: {
        ...globals.jest,
        vi: 'readonly',
      },
    },
  },
];