import { defineConfig, globalIgnores } from "eslint/config";
import nextVitals from "eslint-config-next/core-web-vitals";
import nextTs from "eslint-config-next/typescript";

const eslintConfig = defineConfig([
  ...nextVitals,
  ...nextTs,
  // Override default ignores of eslint-config-next.
  globalIgnores([
    // Default ignores of eslint-config-next:
    ".next/**",
    "out/**",
    "build/**",
    "next-env.d.ts",
    // Agent tooling: skills and per-agent worktrees, each holding a full copy
    // of the repository. They are in .gitignore, which ESLint does not read, so
    // without this every warning is reported once per worktree.
    ".agents/**",
    ".claude/**",
    ".codebuddy/**",
    ".continue/**",
    ".junie/**",
    ".kiro/**",
    "tmp/**",
  ]),

  // Layering is law, from the architecture document: business logic lives in
  // src/services/, and Server Actions and Route Handlers are thin adapters over
  // it. A screen that reaches for the database directly is how a rule ends up
  // enforced in one place and forgotten in three others.
  //
  // The db folder is exempt because it is where Drizzle is defined rather than
  // used: a schema cannot avoid importing the thing it is written in.
  // drizzle.config.ts is the toolchain's own file.
  //
  // Both lib/ and src/ spellings are listed. The architecture document says
  // src/services/, the scaffold put everything at the root, and the schema
  // landed in lib/db/. The letter matters less than the layering, and settling
  // the layout is its own decision rather than something a lint rule should
  // force at the moment somebody trips over it.
  //
  // lib/ is deliberately not exempt. What lives there is pure, and the day
  // something in it wants a database is the day it belongs in a service.
  {
    files: ["**/*.ts", "**/*.tsx"],
    ignores: [
      "lib/db/**",
      "lib/services/**",
      "src/db/**",
      "src/services/**",
      "drizzle.config.ts",
    ],
    rules: {
      "no-restricted-imports": [
        "error",
        {
          patterns: [
            {
              group: ["drizzle-orm", "drizzle-orm/*", "postgres", "pg"],
              message:
                "Reach the database through src/services/. Only src/db/ defines Drizzle.",
            },
          ],
        },
      ],
    },
  },
]);

export default eslintConfig;
