/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  experimental: { 
    missingSuspenseWithCSRBailout: false
  },
  // Ignorer les warnings ESLint durant le build pour Vercel
  eslint: {
    ignoreDuringBuilds: true,
  },
  
  // 🔒 SÉCURITÉ : Headers de sécurité avec CSP en mode Report-Only
  async headers() {
    return [
      {
        source: '/:path*',
        headers: [
          {
            key: 'Content-Security-Policy-Report-Only',
            value: [
              // Scripts JavaScript
              "default-src 'self'",
              "script-src 'self' 'unsafe-inline' 'unsafe-eval' https://eu-assets.i.posthog.com https://eu.i.posthog.com",
              
              // Styles CSS
              "style-src 'self' 'unsafe-inline'",
              
              // Images
              "img-src 'self' data: blob: https: https://*.s3.eu-west-3.amazonaws.com",
              
              // Fonts
              "font-src 'self' data:",
              
              // Connexions réseau (API, WebSocket, etc.)
              "connect-src 'self' " +
              "https://eu.i.posthog.com " +
              "https://eu-assets.i.posthog.com " +
              "https://*.supabase.co " +
              "wss://*.supabase.co " +
              "https://*.s3.eu-west-3.amazonaws.com " +
              "https://*.lambda-url.eu-west-3.on.aws " +
              "https://api.pdfmonkey.io " +
              "https://api.docuseal.com " +
              "https://api.docuseal.eu",
              
              // Frames (iframes)
              "frame-ancestors 'none'",
              "frame-src 'self' blob:",
              
              // Base URI
              "base-uri 'self'",
              
              // Formulaires
              "form-action 'self'",
              
              // Media
              "media-src 'self' blob:",
              
              // Workers
              "worker-src 'self' blob:",
              
              // Objects (Flash, Java, etc.)
              "object-src 'none'",
              
              // Rapport des violations
              "report-uri /api/csp-report",
              
              // Forcer HTTPS
              "upgrade-insecure-requests"
            ].join('; ')
          },
          {
            key: 'X-Frame-Options',
            value: 'SAMEORIGIN'
          },
          {
            key: 'X-Content-Type-Options',
            value: 'nosniff'
          },
          {
            key: 'Referrer-Policy',
            value: 'strict-origin-when-cross-origin'
          },
          {
            key: 'Permissions-Policy',
            value: 'geolocation=(), microphone=(), camera=(), payment=()'
          },
          {
            key: 'X-XSS-Protection',
            value: '1; mode=block'
          }
        ]
      }
    ]
  },
  
  // Configuration pour optimiser les chunks et éviter les erreurs de modules Supabase
  webpack: (config, { dev, isServer }) => {
    if (!isServer) {
      // Ignorer le module 'canvas' côté client (optionnel pour pdfjs-dist)
      config.resolve.fallback = {
        ...config.resolve.fallback,
        canvas: false,
      };

      // Optimiser les chunks pour éviter les problèmes avec Supabase
      config.optimization.splitChunks = {
        ...config.optimization.splitChunks,
        cacheGroups: {
          ...config.optimization.splitChunks.cacheGroups,
          supabase: {
            test: /[\\/]node_modules[\\/]@supabase[\\/]/,
            name: 'supabase',
            chunks: 'all',
            priority: 30,
          },
        },
      };
    }
    return config;
  },
  // Rewrites pour proxier les requêtes PostHog
  async rewrites() {
    return [
      {
        source: '/ingest/static/:path*',
        destination: 'https://eu-assets.i.posthog.com/static/:path*',
      },
      {
        source: '/ingest/:path*',
        destination: 'https://eu.i.posthog.com/:path*',
      },
    ];
  },
  // Nécessaire pour supporter les requêtes API PostHog avec slash final
  skipTrailingSlashRedirect: true,
};

export default nextConfig;
