import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";

/**
 * Middleware passes the request pathname into a custom header so server
 * components (notably the global not-found.tsx) can look up Redirect entries
 * without needing to read the URL via APIs that don't exist on Next 16.
 *
 * We also short-circuit static asset paths to keep the middleware cheap.
 */
const SKIP_PREFIXES = [
  "/_next",
  "/api/",
  "/uploads/",
  "/favicon.ico",
  "/robots.txt",
  "/sitemap.xml",
  "/icon.svg",
  "/feed.xml",
];

export function proxy(req: NextRequest) {
  const path = req.nextUrl.pathname;
  if (SKIP_PREFIXES.some((p) => path.startsWith(p))) {
    return NextResponse.next();
  }
  const headers = new Headers(req.headers);
  headers.set("x-finzone-pathname", path);
  return NextResponse.next({ request: { headers } });
}

export const config = {
  matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
};
