import type { NextConfig } from "next";
/** Where the CMS API lives. The browser never calls it directly (no CORS, and
 *  the refresh cookie is SameSite=Lax); it goes through the rewrite below. */
import { API_ORIGIN } from "./lib/apiOrigin";

const nextConfig: NextConfig = {
  // Let the dev server be opened from other devices on the local network
  // (e.g. http://192.168.31.41:3000). Without this, Next blocks its dev
  // scripts for that address, the page never becomes interactive, and
  // "Sign in" just reloads the login page.
  allowedDevOrigins: ["192.168.*.*"],
  // Same-origin proxy: /api/v1/* on this app → the CMS API. Cookies set by the
  // API (refresh_token, Path=/api/v1/auth) are stored for this app's origin,
  // so the website and the admin share one session.
  rewrites() {
    return [{ source: "/api/v1/:path*", destination: `${API_ORIGIN}/api/v1/:path*` }];
  },
  // The site used to be static files (/index.html, /pages/about.html …).
  // Keep old links and search results working.
  redirects() {
    return [
      { source: "/index.html", destination: "/", permanent: true },
      // The single service detail page became one page per division (/services/<slug>).
      { source: "/service-details", destination: "/services/biotech-nextgen-food-medical-nutrition", permanent: true },
      { source: "/pages/service-details.html", destination: "/services/biotech-nextgen-food-medical-nutrition", permanent: true },
      { source: "/pages/:slug.html", destination: "/:slug", permanent: true },
    ];
  },
  experimental: {
    agentFeedback: true,
  },
  cacheComponents: true,
  partialPrefetching: true,
};

export default nextConfig;
