const nextJest = require("next/jest");

const createJestConfig = nextJest({
  // Provide the path to your Next.js app to load next.config.js and .env files in your test environment
  dir: "./",
});

// Add any custom config to be passed to Jest
const customJestConfig = {
  setupFilesAfterEnv: ["<rootDir>/jest.setup.js"],
  testEnvironment: "jest-environment-jsdom",
  moduleNameMapper: {
    // Mock database and service clients for unit tests
    // Both @/ alias and relative path imports must resolve to mocks
    "^@/lib/redis$": "<rootDir>/__mocks__/lib/redis.js",
    "(.*)/lib/redis$": "<rootDir>/__mocks__/lib/redis.js",
    // convex-client: NOT mapped here — tests that use Convex mock it inline with jest.mock()
    // Mock ES modules that cause issues
    "^@auth/core$": "<rootDir>/__mocks__/@auth/core.js",
    "^@paralleldrive/cuid2$": "<rootDir>/__mocks__/cuid2.js",
    // Default module mapping
    "^@/(.*)$": "<rootDir>/$1",
  },
  transformIgnorePatterns: [
    "/node_modules/(?!(jose|uuid|next-auth|@auth|@paralleldrive|cuid2|@clerk|@google)/)", // Transform ESM modules
  ],
  collectCoverageFrom: [
    // API routes and server actions (business logic)
    "app/api/**/*.{js,jsx,ts,tsx}",

    // Components (UI logic)
    "components/**/*.{js,jsx,ts,tsx}",

    // Utilities and business logic
    "lib/**/*.{js,jsx,ts,tsx}",

    // Exclusions
    "!**/*.d.ts",
    "!**/node_modules/**",
    "!**/.next/**",
    "!**/coverage/**",
    "!**/tests/**",
    "!**/lib/generated/**", // Exclude Prisma generated code (39MB)
    "!**/lib/config/translations/**", // Exclude translation data (5,979 lines)
    "!**/app/**/layout.tsx", // Layouts are E2E tested
    "!**/app/**/page.tsx", // Pages are E2E tested
    "!**/app/**/loading.tsx", // Loading states are E2E tested
    "!**/app/**/error.tsx", // Error pages are E2E tested
    "!**/app/**/not-found.tsx", // 404 pages are E2E tested
  ],
  coverageThreshold: {
    global: {
      branches: 90,
      functions: 90,
      lines: 90,
      statements: 90,
    },
  },
  testMatch: ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"],
  testPathIgnorePatterns: [
    "/node_modules/",
    "/.next/",
    "/tests/",
    "node_modules_old_corrupt",
    "\\.claude/worktrees",
    "\\.claude/mcp",
  ],
  modulePathIgnorePatterns: [
    "node_modules_old_corrupt",
    "\\.claude/worktrees",
    "\\.claude/mcp",
    "<rootDir>/backups",
  ],
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
// We must override the resolved config because next/jest injects a blanket
// "/node_modules/" transformIgnorePatterns entry that blocks ALL node_modules
// transforms, making our exception-based pattern ineffective.
const baseConfig = createJestConfig(customJestConfig);
module.exports = async () => {
  const resolved = await baseConfig();
  // Replace the default patterns with only our exception-based pattern + CSS modules
  resolved.transformIgnorePatterns = [
    "/node_modules/(?!(jose|uuid|next-auth|@auth|@paralleldrive|cuid2|@clerk|@google)/)",
    "^.+\\.module\\.(css|sass|scss)$",
  ];
  return resolved;
};
