import type { NextConfig } from "next";

// BACKEND_URL: resolved at build time for rewrites. Must be resolvable from the container.
// In Docker: passed as build arg (http://backend:8000). Local dev: http://127.0.0.1:8000
const backendUrl = (process.env.BACKEND_URL || "http://127.0.0.1:8000").replace(/\/$/, "");

const nextConfig: NextConfig = {
  // Docker: standalone output for optimized production images.
  // On Windows without Developer Mode / admin rights the standalone symlink
  // copy can fail with EPERM. Set NEXT_OUTPUT=default to disable standalone.
  output: process.env.NEXT_OUTPUT === "default" ? undefined : "standalone",
  // Turbopack ne résout pas le require() dynamique de lightningcss → "Module not found".
  // Toujours lancer avec Webpack : npm run dev ou next dev --webpack (pas "next dev" seul).
  serverExternalPackages: ["lightningcss", "@tailwindcss/postcss", "@tailwindcss/node"],
  async rewrites() {
    return [
      {
        source: "/api/:path*",
        destination: `${backendUrl}/api/:path*`,
      },
    ];
  },
};

export default nextConfig;
