/* eslint-disable import-x/no-named-as-default-member */
import js from '@eslint/js';
import tseslint from 'typescript-eslint';
import importPlugin from 'eslint-plugin-import-x';
import eslintConfigPrettier from 'eslint-config-prettier';
import globals from 'globals';

export default tseslint.config(
  // Global ignores
  {
    ignores: [
      '**/node_modules/**',
      '**/dist/**',
      '**/.next/**',
      '**/coverage/**',
      '**/*.js',
      '**/*.cjs',
      '**/*.mjs',
      '!eslint.config.mjs',
      '**/next-env.d.ts',
    ],
  },

  // Base JS recommended rules
  js.configs.recommended,

  // TypeScript recommended rules
  ...tseslint.configs.recommended,

  // Import plugin
  importPlugin.flatConfigs.recommended,
  importPlugin.flatConfigs.typescript,

  // Prettier (disables conflicting rules)
  eslintConfigPrettier,

  // Shared settings for all TS files
  {
    files: ['**/*.ts', '**/*.tsx'],
    languageOptions: {
      globals: {
        ...globals.node,
      },
    },
    settings: {
      'import-x/resolver': {
        typescript: {
          project: ['apps/*/tsconfig.json', 'tsconfig.base.json'],
        },
      },
    },
    rules: {
      // TypeScript
      '@typescript-eslint/no-unused-vars': [
        'error',
        { argsIgnorePattern: '^_', varsIgnorePattern: '^_' },
      ],
      '@typescript-eslint/no-explicit-any': 'warn',
      '@typescript-eslint/consistent-type-imports': [
        'error',
        { prefer: 'type-imports', fixStyle: 'inline-type-imports' },
      ],

      // Import ordering
      'import-x/order': [
        'error',
        {
          groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index'],
          'newlines-between': 'never',
          alphabetize: { order: 'asc', caseInsensitive: true },
        },
      ],
      'import-x/no-duplicates': ['error', { 'prefer-inline': true }],
      'import-x/no-unresolved': 'off', // TypeScript handles this

      // General
      'no-console': ['warn', { allow: ['warn', 'error'] }],
    },
  },

  // NestJS-specific rules for the API app
  {
    files: ['apps/api/**/*.ts', 'src/modules/**/*.ts'],
    rules: {
      // NestJS uses empty classes for modules, allow them
      '@typescript-eslint/no-extraneous-class': 'off',
      // NestJS decorators require this pattern
      '@typescript-eslint/no-unsafe-declaration-merging': 'off',
    },
  },

  // Module encapsulation: prevent cross-module internal imports
  // (excludes test files — tests may import internals directly for unit testing)
  {
    files: ['apps/api/src/modules/**/*.ts'],
    ignores: ['**/*.spec.ts', '**/*.test.ts', '**/__tests__/**'],
    rules: {
      'no-restricted-imports': [
        'error',
        {
          patterns: [
            {
              group: [
                '@modules/*/application/*',
                '@modules/*/domain/*',
                '@modules/*/infrastructure/*',
                '@modules/*/presentation/*',
              ],
              message:
                'Import from the module barrel (@modules/<module>) instead of internal paths. This preserves module encapsulation.',
            },
          ],
        },
      ],
    },
  },

  // React/Next.js overrides for web app
  {
    files: ['apps/web/**/*.ts', 'apps/web/**/*.tsx'],
    languageOptions: {
      globals: {
        ...globals.browser,
        React: 'readonly',
      },
    },
    rules: {
      'no-console': 'error',
    },
  },

  // Test files
  {
    files: ['**/*.spec.ts', '**/*.test.ts', '**/__tests__/**/*.ts'],
    rules: {
      '@typescript-eslint/no-explicit-any': 'off',
      'no-console': 'off',
    },
  },

  // Script files (seeds, migrations, etc.)
  {
    files: ['prisma/**/*.ts'],
    rules: {
      'no-console': 'off',
      '@typescript-eslint/no-unused-vars': 'warn',
    },
  },
);
