import { withSentryConfig } from "@sentry/nextjs";
import type { NextConfig } from "next";
import createNextIntlPlugin from "next-intl/plugin";

const nextConfig: NextConfig = {
  /* config options here */
  // React Compiler runs via babel-plugin-react-compiler — a full Babel pass on
  // every file. In dev (Turbopack) that dominates cold-compile + HMR latency for
  // little benefit, so gate it to the production build only (where the runtime
  // memoization actually matters). Dev stays on SWC = much faster compile.
  reactCompiler: process.env.NODE_ENV === "production",
  // better-auth lazily `import()`s sqlite dialects (e.g. @better-auth/kysely-adapter's
  // bun-sqlite dialect) whose `import { DEFAULT_MIGRATION_LOCK_TABLE } from "kysely"`
  // is invalid in kysely 0.29. Node never loads that path (we use Postgres/drizzle),
  // but Turbopack eagerly static-validates it and fails the build. Keep these server
  // libs external so they load at runtime as plain Node modules instead of bundling.
  serverExternalPackages: [
    "import-core",
    "semantic-core",
    "better-auth",
    "@better-auth/kysely-adapter",
    // The MCP route uses mcp-handler, which imports `redis` at module load (only
    // used on the SSE path; our route is stateless streamable HTTP). Keep both
    // external so they load as plain Node modules instead of being bundled.
    "mcp-handler",
    "redis",
  ],
  // Tree-shake barrel packages (icons / radix) so a single named import doesn't
  // pull the whole library into the client bundle.
  experimental: {
    optimizePackageImports: ["lucide-react", "radix-ui"],
  },
  // Densify the dev server timeline (d3k aggregates it): log full fetch URLs
  // (Firecrawl crawl, etc.). Server Functions + incoming requests are already
  // logged by default in dev.
  logging: {
    fetches: { fullUrl: true },
  },
};

const withNextIntl = createNextIntlPlugin("./src/i18n/request.ts");
export default withSentryConfig(withNextIntl(nextConfig), {
  // For all available options, see:
  // https://www.npmjs.com/package/@sentry/webpack-plugin#options

  org: "seo-insight",

  project: "javascript-nextjs",

  // Only print logs for uploading source maps in CI
  silent: !process.env.CI,

  // For all available options, see:
  // https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/

  // Upload a larger set of source maps for prettier stack traces (increases build time)
  widenClientFileUpload: true,

  // Route browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers.
  // This can increase your server load as well as your hosting bill.
  // Note: Check that the configured route will not match with your Next.js middleware, otherwise reporting of client-
  // side errors will fail.
  tunnelRoute: "/monitoring",

  webpack: {
    // Enables automatic instrumentation of Vercel Cron Monitors. (Does not yet work with App Router route handlers.)
    // See the following for more information:
    // https://docs.sentry.io/product/crons/
    // https://vercel.com/docs/cron-jobs
    automaticVercelMonitors: true,

    // Tree-shaking options for reducing bundle size
    treeshake: {
      // Automatically tree-shake Sentry logger statements to reduce bundle size
      removeDebugLogging: true,
    },
  },
});
