module.exports = {
  env: {
    node: true,
    es2021: true,
  },
  extends: 'eslint:recommended',
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'commonjs',
  },
  rules: {
    // Error Prevention
    'no-unused-vars': ['warn', { argsIgnorePattern: '^_', varsIgnorePattern: '^_' }],
    'no-console': 'off', // We use structured logging, but console is okay for debug
    'no-undef': 'error',
    'no-unreachable': 'error',
    'no-constant-condition': ['error', { checkLoops: false }],

    // Code Quality
    'prefer-const': 'warn',
    'no-var': 'warn',
    'eqeqeq': ['warn', 'always', { null: 'ignore' }],
    'curly': ['warn', 'multi-line'],
    'no-throw-literal': 'error',

    // Async/Await
    'require-await': 'warn',
    'no-async-promise-executor': 'error',
    'no-await-in-loop': 'off', // Sometimes intentional for sequential operations

    // Style (Prettier handles formatting, these are semantic)
    'consistent-return': 'off', // Express routes don't always return
    'no-nested-ternary': 'warn',
    'max-depth': ['warn', 4],
    'complexity': ['warn', 20],

    // Prevent common pitfalls
    'no-eval': 'error',
    'no-implied-eval': 'error',
    'no-new-func': 'error',
    'no-with': 'error',
    'no-proto': 'error',
  },
  overrides: [
    {
      // Test files can be more lenient
      files: ['**/__tests__/**/*.js', '**/*.test.js', '**/*.spec.js'],
      env: {
        jest: true,
      },
      rules: {
        'no-unused-expressions': 'off',
        'max-depth': 'off',
      },
    },
    {
      // Browser-side assets (client JS)
      files: ['assets/**/*.js', 'frontend/**/*.js'],
      env: {
        browser: true,
        es2021: true,
        node: false,
      },
      globals: {
        // Common dashboard globals from status/index.html context
        apiUrl: 'readonly',
        API_BASE_URL: 'readonly',
        CONFIG: 'readonly',
        // Client-side dashboard classes (loaded via script tags)
        ErrorHandler: 'readonly',
        ProgressTracker: 'readonly',
        ThemeAdapter: 'readonly',
        DnsTemplateSelector: 'readonly',
        TourManager: 'readonly',
        TooltipDefinitions: 'readonly',
      },
      rules: {
        'no-undef': 'warn',
      },
    },
  ],
};
