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

export function middleware(req: NextRequest) {
  const isPrefetch =
    req.headers.get("next-router-prefetch") === "1" ||
    req.headers.get("purpose") === "prefetch";

  // prefetch بدون کوکی نباید redirect به /login کش شود
  if (isPrefetch) {
    return NextResponse.next();
  }

  const token = req.cookies.get("token")?.value?.trim();

  if (req.nextUrl.pathname.startsWith("/settings") && !token) {
    const loginUrl = new URL("/login", req.url);
    loginUrl.searchParams.set("redirect", req.nextUrl.pathname);
    return NextResponse.redirect(loginUrl);
  }

  return NextResponse.next();
}
//
export const config = {
  matcher: ["/settings/:path*"],
};
 