/** @type {import("next").NextConfig} */
const nextConfig = {
  transpilePackages: ["@codeflash-ai/common"],
  webpack: (config, { isServer }) => {
    config.watchOptions = {
      poll: 1000,
      aggregateTimeout: 300,
    }

    // Handle web-tree-sitter's Node.js module imports in browser.
    // fallback handles static require(); alias handles dynamic import()
    if (!isServer) {
      config.resolve.fallback = {
        ...config.resolve.fallback,
        fs: false,
        "fs/promises": false,
        path: false,
        module: false,
      }
      config.resolve.alias = {
        ...config.resolve.alias,
        module: false,
      }
    }

    return config
  },
  experimental: {
    serverActions: {
      allowedOrigins: ["app.codeflash.ai", "localhost:3000"],
      bodySizeLimit: '5mb', // Increased from default 1mb to handle large PR creation payloads
    },
    instrumentationHook: true,
  },
  typescript: {
    ignoreBuildErrors: false,
  },
  // Optimize for production stability
  poweredByHeader: false,
  compress: true,
  images: {
    remotePatterns: [
      {
        protocol: "https",
        hostname: "avatars.githubusercontent.com",
      },
      {
        protocol: "https",
        hostname: "github.com",
      },
    ],
  },
}

// module.exports = nextConfig

import { withSentryConfig } from "@sentry/nextjs"

export default withSentryConfig(
  nextConfig,
  {
    // For all available options, see:
    // https://github.com/getsentry/sentry-webpack-plugin#options

    // Suppresses source map uploading logs during build
    silent: true,
    org: "codeflash-ai",
    project: "webapp",
  },
  {
    // 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,

    // Routes browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers (increases server load)
    tunnelRoute: "/monitoring",

    // Hides source maps from generated client bundles
    hideSourceMaps: true,

    // Automatically tree-shake Sentry logger statements to reduce bundle size
    disableLogger: true,

    // Disable automatic instrumentation that might cause issues
    automaticVercelMonitors: false,
  },
)
