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';

// Playwright 全局变量
const playwrightGlobals = {
  test: 'readonly',
  expect: 'readonly',
  describe: 'readonly',
  beforeAll: 'readonly',
  afterAll: 'readonly',
  beforeEach: 'readonly',
  afterEach: 'readonly',
  page: 'readonly',
  browser: 'readonly',
  context: 'readonly',
  request: 'readonly',
};

export default [
  // 基础配置
  {
    files: ['**/*.{js,jsx,ts,tsx}'],
    ignores: [
      'node_modules/**',
      '*/node_modules/**',
      'dist/**',
      '*/dist/**',
      '*.config.js',
      '*.config.ts',
      'scripts/**',
      'coverage/**',
      '*/coverage/**',
      'build/**',
      '*/build/**',
    ],
    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',
      },
    },
  },

  // E2E 测试环境配置 (Playwright)
  {
    files: ['**/tests/e2e/**/*.{js,ts,jsx,tsx}'],
    languageOptions: {
      ecmaVersion: 'latest',
      sourceType: 'module',
      parser: typescriptParser,
      globals: {
        ...globals.node,
        ...playwrightGlobals,
      },
    },
    rules: {
      // TypeScript 规则
      '@typescript-eslint/no-unused-vars': ['error', {
        argsIgnorePattern: '^_',
        varsIgnorePattern: '^_',
        caughtErrorsIgnorePattern: '^_',
      }],
      '@typescript-eslint/no-explicit-any': 'warn',
      '@typescript-eslint/explicit-function-return-type': 'off',

      // 捕获冗余的 null 检查
      'no-constant-binary-expression': 'error',

      // 捕获空 catch 块
      'no-empty': ['error', { allowEmptyCatch: false }],

      // 首选 const
      'prefer-const': 'error',

      // 允许 console.debug 和 console.warn，但不允许 console.log
      'no-console': ['error', { allow: ['debug', 'warn', 'error'] }],

      // 其他基础规则
      'no-undef': 'off',
      'no-unused-vars': 'off',
    },
  },
];
