// Flat ESLint config (ESLint 10 / Next.js 16).
//
// `next lint` was removed in Next 16, so linting runs through the ESLint CLI
// directly: `npm run lint`. The eslint-config-next package (v16) ships a
// ready-made flat-config array that wires up the Next core-web-vitals rules,
// the TypeScript rules, react / react-hooks / jsx-a11y / import plugins and the
// TypeScript parser — so we just spread it and layer our project rules on top.

import next from 'eslint-config-next';
import prettier from 'eslint-config-prettier/flat';

// Unused variables/imports are dead code. eslint-config-next already enables
// `@typescript-eslint/no-unused-vars` at 'warn'; we raise it to 'error' so
// `npm run check` fails on dead code instead of merely warning. We patch the
// severity *in place* on next's own config objects rather than adding a separate
// override object, because in flat config a rule can only be referenced from a
// config object that registers its plugin — and the `@typescript-eslint` plugin
// is registered inside next's objects, not ours. Prefix a name with `_` to opt
// out (intentionally-unused args, catch bindings, rest-spread siblings).
const NO_UNUSED_VARS = [
  'error',
  {
    args: 'after-used',
    argsIgnorePattern: '^_',
    varsIgnorePattern: '^_',
    caughtErrorsIgnorePattern: '^_',
    ignoreRestSiblings: true,
  },
];

const nextWithStrictUnusedVars = [...next].map((entry) =>
  entry?.rules?.['@typescript-eslint/no-unused-vars']
    ? { ...entry, rules: { ...entry.rules, '@typescript-eslint/no-unused-vars': NO_UNUSED_VARS } }
    : entry
);

/** @type {import('eslint').Linter.Config[]} */
const config = [
  // Things ESLint should never look at.
  {
    ignores: ['.next/**', 'out/**', 'coverage/**', 'node_modules/**', 'next-env.d.ts'],
  },

  // Next.js + TypeScript + React + import/a11y rule sets, with no-unused-vars
  // raised to error (see NO_UNUSED_VARS above).
  ...nextWithStrictUnusedVars,

  // Turn off every stylistic rule that would fight Prettier. Keep this last
  // among the rule-providing entries so it wins. Formatting is owned by
  // Prettier (`npm run format`), never by ESLint.
  prettier,

  // NOTE: `import/no-cycle` is intentionally NOT enabled. On this toolchain the
  // eslint-plugin-import TypeScript resolver bundled by eslint-config-next 16
  // throws "invalid interface loaded as resolver" for that rule, and it cannot
  // follow the `@/*` path alias to trace cycles anyway. Re-add it once the
  // import-resolver-typescript interface is compatible.
];

export default config;
