module.exports = {
	parser: '@typescript-eslint/parser',
	extends: [
		'eslint:recommended',
		'plugin:@typescript-eslint/recommended',
		'plugin:@typescript-eslint/recommended-requiring-type-checking',
		'prettier',
	],
	plugins: ['@typescript-eslint'],
	parserOptions: {
		ecmaVersion: 2022,
		sourceType: 'module',
		project: './tsconfig.json',
		tsconfigRootDir: __dirname,
	},
	env: {
		node: true,
		es2022: true,
		jest: true,
	},
	rules: {
		// TypeScript specific rules
		'@typescript-eslint/no-namespace': ['error', { allowDeclarations: true }],
		'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_', varsIgnorePattern: '^_' }],
		'@typescript-eslint/explicit-function-return-type': 'off',
		'@typescript-eslint/explicit-module-boundary-types': 'off',
		'@typescript-eslint/no-explicit-any': 'off',
		'@typescript-eslint/no-non-null-assertion': 'off',
		'@typescript-eslint/prefer-nullish-coalescing': 'off',
		'@typescript-eslint/prefer-optional-chain': 'off',
		'@typescript-eslint/no-floating-promises': 'off',
		'@typescript-eslint/await-thenable': 'off',
		'@typescript-eslint/no-misused-promises': 'off',
		'@typescript-eslint/require-await': 'off',
		'@typescript-eslint/no-unnecessary-type-assertion': 'off',
		'@typescript-eslint/no-inferrable-types': 'off',
		'@typescript-eslint/ban-ts-comment': 'off',
		'@typescript-eslint/no-unsafe-member-access': 'off',
		'@typescript-eslint/no-unsafe-argument': 'off',
		'@typescript-eslint/restrict-template-expressions': 'off',
		'@typescript-eslint/no-unsafe-call': 'off',
		'@typescript-eslint/no-unsafe-return': 'off',
		'@typescript-eslint/no-unsafe-assignment': 'off',
		'@typescript-eslint/only-throw-error': 'off',
		'@typescript-eslint/no-require-imports': 'off',
		'@typescript-eslint/no-base-to-string': 'off',
		'@typescript-eslint/no-unsafe-enum-comparison': 'off',
		'@typescript-eslint/no-redundant-type-constituents': 'off',
		'no-useless-escape': 'off',
		'no-useless-catch': 'off',

		// General rules
		'no-console': 'off', // Allow console.log in backend
		'no-debugger': 'error',
		'prefer-const': 'error',
		'no-var': 'error',
		eqeqeq: 'off',
		'no-eval': 'error',
		'no-implied-eval': 'error',
		'no-new-func': 'error',
		'no-script-url': 'error',
		'no-void': ['error', { allowAsStatement: true }],
		'prefer-promise-reject-errors': 'error',
		'require-await': 'off', // Handled by @typescript-eslint

		// Code complexity rules (temporarily disabled)
		complexity: 'off',
		'max-depth': 'off',
		'max-params': 'off',
		'max-statements': 'off',
	},
	ignorePatterns: ['dist/', 'node_modules/', 'storage/', 'coverage/', '**/*.js'],
	overrides: [
		{
			files: ['src/config/**/*.ts'],
			rules: {
				'no-restricted-syntax': 'off', // Allow process.env in config layer
			},
		},
		{
			files: ['src/**/*.ts'],
			excludedFiles: ['src/config/**/*.ts', 'src/root.ts'],
			rules: {
				'no-restricted-syntax': [
					'error',
					{
						selector: 'MemberExpression[object.name="process"][property.name="env"]',
						message:
							'Use Config from #config instead of process.env directly. Environment variables should only be accessed through the config layer.',
					},
				],
			},
		},
		{
			files: ['**/*.test.ts', '**/*.spec.ts', 'tests/**/*.ts'],
			rules: {
				'@typescript-eslint/no-explicit-any': 'off',
				'@typescript-eslint/no-non-null-assertion': 'off',
				'no-console': 'off',
				'no-restricted-syntax': 'off', // Allow process.env in tests
			},
		},
		{
			files: ['src/middlewares/csrf.ts', 'src/middlewares/rate-limiter.ts'],
			rules: {
				'no-restricted-syntax': 'off', // Allow process.env.NODE_ENV for environment detection in security middleware
			},
		},
	],
};
