import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  /* config options here */
  turbopack: {
    root: __dirname,
    resolveAlias: {
      '@': __dirname,
    },
  },
  // Disable compression to allow SSE streaming to work properly
  // Without this, Next.js buffers responses and sends them all at once
  compress: false,
  
  // Optimize package imports for better tree shaking
  // Note: modularizeImports for @phosphor-icons/react was removed because
  // it conflicts with optimizePackageImports and the SSR files use named exports
  experimental: {
    optimizePackageImports: [
      '@phosphor-icons/react',
      '@radix-ui/react-dialog',
      '@radix-ui/react-dropdown-menu',
      '@radix-ui/react-select',
      '@radix-ui/react-tabs',
      '@radix-ui/react-tooltip',
      'motion',
      'ai',
      '@ai-sdk/react',
    ],
  },
  
  // Externalize heavy packages to reduce serverless function size
  // These packages won't be bundled into each function, reducing the ~250MB limit issue
  serverExternalPackages: [
    // Syntax highlighting - shiki bundles ALL language grammars (~30MB+)
    'shiki',
    'rehype-pretty-code',
    
    // Monaco editor - heavy code editor
    'monaco-editor',
    '@monaco-editor/react',
    
    // Mermaid - diagram generation library
    'mermaid',
    
    // HTTP snippet generation
    '@hoppscotch/httpsnippet',
  ],
  
  // Exclude cache and large directories from output file tracing
  // This prevents webpack cache and other large files from being included in serverless functions
  outputFileTracingExcludes: {
    '*': [
      // Next.js cache directories
      '.next/cache/**',
      '.next/cache/webpack/**',
      
      // Large node_modules that shouldn't be traced
      'node_modules/@swc/**',
      'node_modules/esbuild/**',
      'node_modules/terser/**',
      'node_modules/webpack/**',
      
      // Note: shiki is NOT excluded because it's needed by /api/docs for syntax highlighting
      // The dynamic import with fallback in route.ts handles cases where it may be unavailable
      
      // Monaco editor assets
      'node_modules/monaco-editor/**',
      
      // TypeScript compiler (not needed at runtime)
      'node_modules/typescript/**',
    ],
  },
};

export default nextConfig;
