import type { NextConfig } from 'next';

// Bundle analyzer
const withBundleAnalyzer = require('@next/bundle-analyzer')({
  enabled: process.env.ANALYZE === 'true',
});

const nextConfig: NextConfig = {
  // ESLint configuration
  eslint: {
    // Do not run ESLint during builds. Lint is enforced separately (CI/PR),
    // while build remains focused on compilation/type correctness.
    ignoreDuringBuilds: true,
  },
  // TypeScript configuration
  // Do not ignore type errors during builds. Builds must fail on type errors.
  typescript: {
    ignoreBuildErrors: false,
  },
  // Image optimization configuration
  images: {
    // Enable image optimization
    formats: ['image/webp', 'image/avif'],

    // Allowed domains for external images
    domains: [
      'firebasestorage.googleapis.com',
      'lh3.googleusercontent.com', // Google profile images
      'avatars.githubusercontent.com', // GitHub profile images
    ],

    // Remote patterns for more flexible matching
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'firebasestorage.googleapis.com',
        pathname: '/v0/b/**',
      },
      {
        protocol: 'https',
        hostname: '*.googleusercontent.com',
        pathname: '/**',
      },
      {
        protocol: 'https',
        hostname: 'avatars.githubusercontent.com',
        pathname: '/**',
      },
    ],

    // Image sizes for optimization
    deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
    imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],

    // Enable lazy loading by default
    loader: 'default',

    // Minimize layout shift
    minimumCacheTTL: 60,
  },

  // Performance optimizations
  experimental: {
    optimizePackageImports: [
      'lucide-react',
      '@radix-ui/react-icons',
      '@robota-sdk/agent-core',
      '@robota-sdk/agent-playground',
    ],
  },

  // Compiler optimizations
  compiler: {
    // Keep console.log for debugging (temporarily disabled)
    removeConsole: false,
  },

  webpack: (config, { isServer }) => {
    if (!isServer) {
      config.resolve = config.resolve ?? {};
      config.resolve.fallback = {
        ...(config.resolve.fallback ?? {}),
        child_process: false,
        fs: false,
        module: false,
        net: false,
        tls: false,
        worker_threads: false,
      };
      // node: protocol modules are not polyfillable in the browser — alias to false (empty module)
      config.resolve.alias = {
        ...(config.resolve.alias ?? {}),
        'node:child_process': false,
        'node:fs': false,
        'node:net': false,
        'node:tls': false,
        'node:worker_threads': false,
      };
      // agent-core compiles `import { randomUUID } from 'node:crypto'` to
      // `import { randomUUID } from 'crypto'` in its browser dist.
      // Next.js/webpack has no polyfill for crypto.randomUUID, so we swap
      // the entire crypto import to a thin browser shim.
      const { NormalModuleReplacementPlugin } = require('webpack');
      config.plugins = [
        ...(config.plugins ?? []),
        new NormalModuleReplacementPlugin(
          /^(node:)?crypto$/,
          require('path').resolve(__dirname, 'src/lib/crypto-browser.js'),
        ),
        new NormalModuleReplacementPlugin(
          /^(node:)?child_process$/,
          require('path').resolve(__dirname, 'src/lib/child-process-browser.js'),
        ),
      ];
    }

    return config;
  },
};

export default withBundleAnalyzer(nextConfig);
